[英]CORS on api of Codechef (using angularjs)
以下网址以 JSON 格式给出任何比赛的排名https://www.codechef.com/api/rankings/OCT17只需将 OCT17 替换为任何比赛代码
我想制作一个网络应用程序,它将获取这个 api 并显示一个自定义排行榜。我尝试使用 angularjs,但它是 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;
});
});
控制台在 chrome 中显示这 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":""}
IE 上没有错误,而 chrome 上有这个问题可以修复还是只是服务器端问题(或他们的偏好)
我也试过 $http.jsonp() 函数。
您不能在客户端浏览器上进行跨域请求 (CORS)。
此 API 仅允许来自https://developers.codechef.com
请求
鉴于您的请求不是来自该域,您被拒绝访问。
CORS 仅由浏览器强制执行。 因此,如果您有自己的后端服务器并向该服务器发出请求,并且您的服务器从他们的服务器请求(称为代理请求),那么您将没问题,因为您将避免 CORS 问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.