2017-10-18 54 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问题。