Foursquare 返回“无访问控制允许源”标头显示错误

Foursquare returns 'No access control allow origin' header present error

本文关键字:显示 错误 许源 返回 访问控制 Foursquare      更新时间:2023-09-26

我正在对四方形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 链接到按钮时它起作用,为什么当对该 URL 进行 GET 调用时它不起作用?

Ajax不允许从一个域到另一个域的请求,除非服务器提供访问控制允许源(CORS(标头。允许在域之间链接。

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

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