angular.js - 同样的执行请求使用 $http 会提示跨域,但使用 jquery 的 ajax 就正常
本文介绍了angular.js - 同样的执行请求使用 $http 会提示跨域,但使用 jquery 的 ajax 就正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问 题
$.ajax({
url: api.regist,
type: 'POST',
dataType: 'json',
data: {
email: $scope.email,
password: $scope.password
}
})
$http({
url: api.regist,
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
data: {
email: $scope.email,
password: $scope.password
}
})
这两个不应该是完全一样的意义嘛?为什么使用 ajax 可以执行成功,但是使用 $http 就提示跨域了呢?
解决方案
postCfg = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
},
transformRequest: function(data) {
return $.param(data);
}
}
app.config([
'$httpProvider',
function($httpProvider) {
$httpProvider.defaults.useXDomain = true;
delete $httpProvider.defaults.headers.common['X-Requested-With'];
}
]}
$http.post(url, {}, postCfg)
自己找到的方法,测速成功
这篇关于angular.js - 同样的执行请求使用 $http 会提示跨域,但使用 jquery 的 ajax 就正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文