我的服务器端是php/mysql。
我正在另一个域的webservice中进行Ajax调用(该域启用了访问控制*)
var postUrl = "http://logical-brains.com/elance_clone/test_login.php";
var postData = {username : "tanmoy" , password : "123456"};我尝试了简单的jQuery:
$.ajax({
type: "POST",
url: postUrl,
data: postData,
dataType: "json",
crossDomain: true,
success: function(data){ console.log(JSON.stringify(data)); }
}); 它工作得很好,我得到了状态代码:200 OK和预期的结果。
但是当我尝试安古拉杰的时候,我会犯以下的错误:
XMLHttpRequest cannot load http://logical-brains.com/elance_clone/test_login.php. Request header field Content-Type is not allowed by Access-Control-Allow-Headers. AngularJs法典:
var myApp = angular.module('myApp', []);
myApp.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
}]);
myApp.controller('MainCtrl', function($scope, $http) {
var postUrl = "http://logical-brains.com/elance_clone/test_login.php";
var postData = {username : "tanmoy" , password : "123456"};
$http({
url: postUrl,
method: "POST",
data: postData,
crossDomain: true
})
.then(function(result) {
console.log("Success", result);
$scope.someVal = JSON.stringify(result);
},
function(response) { // optional
console.log("error "+response);
}
); }; });发布于 2015-05-31 05:26:34
我遇到问题了。通过深入的搜索我得到..。对于跨域请求,将内容类型设置为application/x-www-form-urlencoded、multipart/form-data或text/plain以外的任何内容类型将触发浏览器向服务器发送飞行前选项请求。因此,如果服务器不允许,它将抛出错误。默认情况下,角内容类型是application/json,它试图发送选项请求。但是jquery的缺省值是application/x-www-form-urlencoded; charset=UTF-8,所以在您的例子中,jquery是有效的,而对于角则是无效的。尝试覆盖角默认标头。或者允许它在服务器端。这是角度样本:
$http.post(url,data,{
headers : {
'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8'
}
});有关详细信息,请参阅http://api.jquery.com/jquery.ajax/内容类型
https://stackoverflow.com/questions/24341290
复制相似问题