代码之家  ›  专栏  ›  技术社区  ›  vijrox

来自jQuery ajax和CORS的Reddit API:请求的资源上不存在“Access Control Allow Origin”标头

  •  2
  • vijrox  · 技术社区  · 11 年前

    我正在尝试使用jQuery的ajax功能访问reddit API。从我在网上看到的情况来看,我使用的代码应该能够执行CORS:

    $.ajax({
        type:"POST",
        url:"http://reddit.com/api/login",
        data: {
            api_type:"json",
            passwd:"MyRedditPassword",
            rem:false,
            user:"MyRedditUsername"
        },
        dataType:"text",
        success:function(data){console.log("success:",data);},
        error:function(error){console.log("error:",error);},
        crossDomain:true,
        xhrFields:{withCredentials:true}
    });
    

    这是访问 this part of reddit's API .

    我得到了错误 No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

    据我了解,这个错误是服务器端的问题;他们对我请求的响应没有CORS所需的报头。这意味着reddit的API不支持CORS。 However, the reddit admins said here that they have implemented CORS support .

    我的请求有什么问题,以至于我试图使用他们表示支持的功能,但我得到的答复是他们不支持?我需要在我的请求中做一些不同的事情来让reddit发送回CORS头吗?

    1 回复  |  直到 11 年前
        1
  •  2
  •   kemitche    11 年前

    从您链接的帖子中:

    除了JSONP,我们现在还支持CORS 未经认证 对reddit API的跨域请求

    强调mine-CORS不支持经过身份验证的请求。