我在使用Angular Js中的$ http.post()發佈數據時遇到此錯誤。Angular js:跨源請求被阻止:同源策略不允許讀取遠程資源
跨源請求被阻止:同源策略不允許在{{post url}}處讀取遠程資源。這可以通過將資源移動到相同的域或啓用CORS來解決。
這是我的代碼:
var ionicAppControllers = angular.module('ionicAppControllers', []);
ionicAppControllers.config(function($httpProvider) {
//Enable cross domain calls
$httpProvider.defaults.useXDomain = true;
//Remove the header containing XMLHttpRequest used to identify ajax call
//that would prevent CORS from working
delete $httpProvider.defaults.headers.common['X-Requested-With'];
});
ionicAppControllers.controller('createItemCtrl', ['$scope', '$http',
function($scope, $http) {
$scope.parts = [];
$scope.add = function() {
$scope.parts.push({
id: '',
code: '',
description: ''
});
};
$scope.submitItemForm = function(isValid) {
if (isValid) {
$scope.postdata = {};
$scope.postdata.item = $scope.item;
$scope.postdata.description = $scope.description;
for (var i in $scope.parts)
{
for (var j in $scope.parts[i])
{
if (j == '$$hashKey')
{
delete($scope.parts[i][j]);
//console.log(j);
}
}
}
$scope.postdata.parts = $scope.parts;
console.log($scope.postdata);
$http({
method: 'POST',
url: 'URL',
data: $scope.postdata, // pass in data as strings
headers: {'Content-Type': 'application/json'}
}).success(function(data) {
if (data.success)
{
alert('sucess');
}
else
{
alert('fail');
}
});
}
};
$scope.add();
}]);
我不知道它是什麼添加此XML代碼...只是寫刪除錯誤... – Napster 2014-10-30 10:24:31