简体   繁体   English

AngularJS访问Cbssports REST API

[英]AngularJS access to Cbssports REST API

I am trying to create an angularjs application as a cbssports plugin. 我正在尝试创建一个angularjs应用程序作为cbssports插件。

They provide a RESTful API. 它们提供了RESTful API。

The following is my $http request: 以下是我的$ http请求:

$http.get(basePath + 'league/owners?access_token=' + cbssportsTokens['access_token'] + '&response_format=JSON')
        .success(function(data) {
           return data;
        });

That is wrapped up in a factory and used in a controller. 它被包装在工厂中并在控制器中使用。 As far as I can tell this is getting executed correctly. 据我所知,这是正确执行的。 When I look at the chrome developer tools I can see the following error on my request from the console: 当我查看chrome开发人员工具时,在控制台发出的请求中我看到以下错误:

OPTIONS http://api.cbssports.com/fantasy/league/owners?access_token=U2FsdGVkX18Hyd0…J9DrpO7C-OXQQNXGMh0ej0iXVfPf5DkQwkLwSpCqGhipd6HogV_gZ&response_format=JSON angular.js:8560
(anonymous function) angular.js:8560
sendReq angular.js:8354
$http.serverRequest angular.js:8087
wrappedCallback angular.js:11572
wrappedCallback angular.js:11572
(anonymous function) angular.js:11658
Scope.$eval angular.js:12701
Scope.$digest angular.js:12513
Scope.$apply angular.js:12805
done angular.js:8378
completeRequest angular.js:8592
xhr.onreadystatechange
XMLHttpRequest cannot load http://api.cbssports.com/fantasy/league/owners?access_token=U2FsdGVkX18xBod…oQWvjDVSbpZCOVsoIKeVXKRSYdo6dBbIE0rgMWTkWhmgPUTyr_xnS&response_format=JSON. The 'Access-Control-Allow-Origin' header has a value 'https://www.cbssports.com' that is not equal to the supplied origin. Origin 'http://xx.xx.xx.xx:9001' is therefore not allowed access. ?access_token=U2FsdGVkX18xBodWEOfeqys5X4aDpghYrE22FGljlJd_TtKRHlWh4LHWFwVxay95BbAWvn4te1foQWvjDVSbp…:1

When I click the link that it said it couldn't load, it takes me to a new page with the expected output!! 当我单击它说无法加载的链接时,它将带我到预期输出的新页面! So clearly CORS shouldn't be an issue here from the server. 因此,很明显,CORS在服务器上不应该成为问题。

I have read many different issues with CORS about changing the headers. 我已经阅读了许多有关更改标题的CORS问题。 The follow is how I have set it up: 以下是我的设置方法:

  $httpProvider.defaults.headers.common['Access-Control-Allow-Origin'] = '*';
  $httpProvider.defaults.headers.common['Access-Control-Allow-Headers'] = 'Origin';

Any advice would be greatly appreciated, also my first post to SO, so please let me know if more info would help. 任何建议将不胜感激,也是我的第一篇帖子,所以,请让我知道更多信息是否有帮助。

So I didn't exactly get it to work. 所以我没有完全使它工作。 But I found a sort of workaround. 但是我找到了一种解决方法。

 function _get(url) {
     return $resource(basePath + url, {
           access_token: cbssportsTokens['access_token'],
           response_format: 'JSON',
           callback: 'JSON_CALLBACK'
     }, {
        get: {
           method: 'JSONP'
        }
     });
  }

I went ahead and used a JSONP request instead of a GET, the cbs server seems to be happier with that. 我继续使用JSONP请求而不是GET,cbs服务器似乎对此更满意。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM