簡體   English   中英

PHP Nginx沒有“ Access-Control-Allow-Origin”錯誤

[英]No 'Access-Control-Allow-Origin' error with PHP Nginx

我在后端使用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 ,但這不能解決我的問題。 我該如何解決?

此錯誤消息是由於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,無法繞開CORS

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM