javascript - Foursquare 返回 'No access control allow origin' header 存在错误

标签 javascript angularjs xmlhttprequest foursquare

我正在对 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/

相关文章:

javascript - Soundcloud Api - 通过歌手获取轨道

javascript - 在 html 控件中创建闭环选项卡顺序

javascript - onMessage 不能在 React Native 中工作

javascript - AngularJS 中的后台服务

angularjs - 当侧面菜单出现时 ionic 前 View 被缓存

javascript - Angular 如何设置输入类型文件的样式

javascript - XMLHttpRequest 从远程主机获取 HTTP 响应

javascript - 为什么我的 XHR 的 statusText 是空的?

css - Adapt-Strap 和列文本对齐

javascript 设置 header Access-Control-Allow-Origin