2014-10-03 74 views
1

我想創建一個angularjs應用程序作爲cbssports插件。AngularJS訪問Cbssports REST API

它們提供了一個RESTful API。

下面是我的$ http請求:

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

即在工廠包裹起來,並在控制器中使用。據我所知,這是正確執行。當我看着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 

當我點擊的鏈接,它說,它無法加載,它帶我到一個新的頁面預期輸出!所以CORS應該不是服務器的問題。

我已經閱讀了許多關於更改標題的CORS的不同問題。該遵循的是我如何設定它:

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

任何意見,將不勝感激,也是我的第一篇來左右,所以請讓我知道如果有更多的信息將有助於。

+0

我使用的全部是'$ httpProvider.defaults.useXDomain = true;'和'delete $ httpProvider.defaults.headers.common ['X-Requested-With'];'使用CORS。雖然,我正在使用$資源。 – Owen 2014-10-03 21:54:01

+0

我也試過,結果相同。 – rbuckley 2014-10-03 22:28:51

回答

0

所以我沒有完全明白它的工作。但我找到了一種解決方法。

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

我繼續使用JSONP請求而不是GET,cbs服務器似乎更快樂。