我正在对 foursquare OAuth URL 进行 GET 调用。浏览器控制台打印以下错误
XMLHttpRequest cannot load https://foursquare.com/oauth2/authenticate?client_id=DF4I1TGNVHAM40PDLOHZQZ…onse_type=code&redirect_uri=http://localhost:9000/auth/foursquare/callback. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:9000' is therefore not allowed access.
这是进行调用的代码片段:
$http.get(url).success(function(data,status,headers,config){
});
但是如果我将按钮链接到此网址,浏览器会将我带到身份验证页面。
这有什么区别?为什么当 URL 链接到按钮时它可以工作,为什么当对该 URL 进行 GET 调用时它不工作?
最佳答案
Ajax 不允许从一个域向另一个域发出请求,除非服务器提供 Access-Control-Allow-Origin (CORS) header 。允许域之间的链接。
Mozilla 有一些关于允许和不允许的内容的有用信息。 https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy#Cross-origin_network_access
- 通常允许跨源写入。例如链接、重定向和表单提交。某些很少使用的 HTTP 请求需要预检。
- 通常允许跨源嵌入,例如图像、脚本。
- 通常不允许跨源读取,例如 ajax 调用
关于javascript - Foursquare 返回 'No access control allow origin' header 存在错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27588823/