【问题标题】:Foursquare returns 'No access control allow origin' header present errorFoursquare 返回“无访问控制允许来源”标头存在错误
【发布时间】:2014-12-21 19:49:56
【问题描述】:

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

这里是发出呼叫的 sn-p:

$http.get(url).success(function(data,status,headers,config){
});

但如果我将一个按钮链接到此 url,浏览器会将我带到身份验证页面。

它有什么不同?为什么当 URL 链接到按钮时它可以工作,为什么当对该 URL 进行 GET 调用时它不工作?

【问题讨论】:

  • 了解 CORS。在这种情况下,您将无法使用 AJAX。

标签: javascript angularjs xmlhttprequest foursquare


【解决方案1】:

Ajax 不允许从一个域到另一个域的请求,除非服务器提供 Access-Control-Allow-Origin (CORS) 标头。允许域之间的链接。

Mozilla 有一些很好的信息来说明什么是允许的,什么是不允许的。 https://developer.mozilla.org/en-US/docs/Web/Security/Same-origin_policy#Cross-origin_network_access

  • 通常允许跨域写入。示例是链接、重定向和表单提交。某些很少使用的 HTTP 请求需要预检。
  • 通常允许跨域嵌入,例如图像、脚本。
  • 通常不允许跨域读取,例如 ajax 调用

【讨论】:

  • 谢谢韦恩。您的回答和您提供的链接帮助我理解了我的问题
猜你喜欢
  • 2020-10-31
  • 2013-03-02
  • 1970-01-01
  • 2017-01-10
相关资源
最近更新 更多