的控制器實現CORS我有一個服務控制器,將來自其它服務器獲取JSON和我來到這個 Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http:somesite.com. This can be fixed by moving the resource to the same domain or enabling CORS.
從角JS
我注意到一些相同的問題,將通過"Access-Control-Allow-Origin": "*"
是修復在我的情況我在我的情況下,我沒有看到任何進展修復它。
這對我的控制器代碼:
var myApp = angular.module('cplanner',[]);
myApp.controller('GetItemCtrl', ['$scope', '$http', function($scope, $http) {
var itemUrl = 'http:somesite.com';
//console.log(itemUrl);
$http({url: itemUrl,
dataType: 'json',
method: 'POST',
dataType : 'json',
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "POST, GET, OPTIONS, PATCH, DELETE",
"Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept"
},
xhrFields: {
withCredentials: true
}}).success(function(data, status, headers, config) {
//check if the response has ok status
if(data.status == "ok"){
console.log(data.data);
$scope.items = data.data;
}
}).error(function(data, status, headers, config) {
});
}]);
我加入了頭,但它似乎並沒有解決我的交叉起源問題。任何人都可以通過關於如何從其他服務器源獲得響應的任何意見和建議來驅動我。 預先感謝您。
我看,但我沒有訪問我的服務器,是有可能在htaccess的這樣做呢? – CaffeineShots 2014-10-30 06:05:35
請參閱http://enable-cors.org/server_apache.html – Tomer 2014-10-30 06:06:42
@caffeineshots您可以使用JSONP或其他東西嗎?你的意思是你的web服務器或API服務器上的htaccess? – John 2014-10-30 06:10:14