【问题标题】:No 'Access-Control-Allow-Origin' error with PHP NginxPHP Nginx 没有“Access-Control-Allow-Origin”错误
【发布时间】:2017-05-14 16:10:05
【问题描述】:

我在后端使用 laravel,并尝试使用 jQuery 向 Instagram api 发送请求,但在 Chrome 浏览器检查器中响应正常时出现此错误

test?code=a3679b3…:1 XMLHttpRequest cannot load https://api.instagram.com/oauth/access_token. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://example.com' is therefore not allowed access.

这是我发送请求的代码

                $.ajax({
                method: 'POST',
                url: 'https://api.instagram.com/oauth/access_token',
                data: {
                    client_id: "client_id",
                    client_secret: "client_secret",
                    grant_type: "authorization_code",
                    redirect_uri: "example.com",
                    code: "{{$code}}"
                }

            });

我尝试在我的 Nginx default.conf 文件中添加 Access-Control-Allow-Origin,但这并没有解决我的问题。我该如何解决?

【问题讨论】:

标签: php jquery nginx


【解决方案1】:

此错误消息是由于 CORS。在您的浏览器中,您的 AJAX 请求应该来自同一来源。因此,如果您使用的是 example.com,Chrome 将仅支持对 example.com 的 AJAX 请求。除非,如果其他域(如 api.instagram.com)具有 Acces-Control-Allow-Origin 标头。您可以在此处阅读更多相关信息:

https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS

您可以尝试从后端访问 Instagram。这样,您的 AJAX 请求将通过同一来源发送到您的后端,并且您的后端可以轻松访问 Instagram 的 API。

你也可以试试dataType: "jsonp",在这个答案中你可以了解更多:

instagram jquery ajax type="GET, can't get around CORS

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2017-12-06
  • 2013-06-13
  • 2017-06-15
  • 2016-02-13
  • 2018-09-19
  • 1970-01-01
  • 1970-01-01
  • 2018-12-12
相关资源
最近更新 更多