2017-10-18 55 views
1

以下網址提供任何比賽的JSON格式的排名 https://www.codechef.com/api/rankings/OCT17 只是任何大賽代碼CORS上Codechef的API(使用angularjs)

我想提出一個web應用程序,它會取這個API的替換OCT17和顯示自定義的使用angularjs leaderboard.I嘗試,但它是CORS錯誤

這是代碼

`var app = angular.module('Ranklist', []); 
app.config(['$httpProvider', function($httpProvider) { 
     $httpProvider.defaults.useXDomain = true; 
     delete $httpProvider.defaults.headers.common['X-Requested-With']; 
     console.log("cross origin allowed"); 
    } 
]); 
app.controller('rank',function($scope,$http){ 
    var uri = 'https://www.codechef.com/api/rankings/COPH2017?sortBy=rank&order=asc&page=1&itemsPerPage=25'; 
    $scope.test = "test data"; 
    console.log("love can hear") 
    $http.get(uri) 
     .then(function(response){ 
     $scope.data = response.data; 
    }); 
});` 

控制檯表示鉻這些2個錯誤

`Failed to load https://www.codechef.com/api/rankings/COPH2017?sortBy=rank&order=asc&page=1&itemsPerPage=25: The 'Access-Control-Allow-Origin' header has a value 'https://developers.codechef.com' that is not equal to the supplied origin. Origin 'http://127.0.0.1:58502' is therefore not allowed access.` 

`angular.js:14525 Possibly unhandled rejection: {"data":null,"status":-1,"config":{"method":"GET","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","url":"https://www.codechef.com/api/rankings/COPH2017?sortBy=rank&order=asc&page=1&itemsPerPage=25","headers":{"Accept":"application/json, text/plain, */*"}},"statusText":""}` 

有上無差錯,而沒有對鉻 這會是個固定的,或者僅僅是一個服務器端的問題(或他們的偏好)

我也嘗試$ http.jsonp()函數。

回答

0

您無法在客戶端瀏覽器上創建跨域請求(CORS)。

此API只允許https://developers.codechef.com

請求鑑於你的要求不是從你被拒絕訪問該域名的到來。

CORS僅由瀏覽器強制執行。因此,如果您擁有自己的後端服務器,並向服務器發出請求並向其服務器發送服務器請求(稱爲代理請求),那麼您將會很好,因爲您將避免CORS問題。