2017-05-14 38 views
0

我爲我的後端使用laravel並嘗試向jQuery發送請求到Instagram api,但我在Chrome瀏覽器檢查器中響應正常時收到此錯誤PHP Nginx沒有'訪問控制 - 允許 - 來源'錯誤

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但沒有解決我的問題。我該如何解決它?

+0

我覺得你可以按照這個鏈接。可能會有所幫助。 [http://stackoverflow.com/questions/6114436/access-control-allow-origin-error-sending-a-jquery-post-to-google-apis](http://stackoverflow.com/questions/6114436/ access-control-allow-origin-error-sending-a-jquery-post-to-google-apis) –

回答

0

此錯誤消息是由於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

+0

添加'dataType:「jsonp」'不起作用。 – Honey

+0

你應該嘗試'方法:'GET''。另請查看本教程,它應該有效:[使用jQuery從Instagram獲取照片](https://rudrastyh.com/javascript/get-photos-from-instagram.html) –

相關問題