2015-07-01 75 views
1

我試圖從instagram獲取JSON文件,並且在製作$ http.get時出現錯誤:請求的資源上沒有'Access-Control-Allow-Origin'標頭 - 錯誤

insta.ctrl:

insta.controler 'instaCtrl' ($scope, $http), -> 
    $http.get('http://api.instagram.com/publicapi/oembed/?url=http://instagr.am/p/fA9uwTtkSN/') 
     .success(data), -> 
      #done ! 
     .error(e), -> 
      #nah ! 

我的Apache2的conf對鉻

Header always set Access-Control-Allow-Origin "*" 
Header always set Access-Control-Allow-Headers "X-Requested-With, Content-Type, Origin, Authorization, Accept, Client-Security-Token, Accept-Encoding" 
Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT" 

錯誤消息:

XMLHttpRequest cannot load #url_of_intagram_api_here. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '' is therefore not allowed access. 

它工作時,我禁用鉻在互聯網安全。 有什麼建議嗎?

+0

重複的問題請參考:HTTP://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present-on-the-requested-資源 –

回答

1

顯然Instagram API不實現CORS。不過他們提供了用於數據檢索的JSONP接口。所以你可以做的是使用jsonp方法:

$http.jsonp('http://api.instagram.com/publicapi/oembed/?url=http://instagr.am/p/fA9uwTtkSN/&callback=JSON_CALLBACK').success(function(data) { 
    $scope.data = data; 
}); 

注意你需要發送callback=JSON_CALLBACK GET參數。

演示:http://plnkr.co/edit/OG1sT7A9OM1hWBqCvSmC?p=preview

+0

哦,我明白了,它正在運行xD,謝謝你:') –

相關問題