今天在做一個登錄頁面的輸入驗證碼功能声搁。一直顯示驗證碼不正確黑竞,可是我仔細(xì)核對了很多次,仍然提示不正確疏旨。我猜想不是我看錯很魂,而是程序哪里有問題,但是也一直找不出原因檐涝。問了我們產(chǎn)品經(jīng)理莫换,才知道霞玄,需要設(shè)置withCredentials屬性
$http({
method:'POST',
url:config.login,
withCredentials: true,//必須要填
headers:{
"Content-Type":"application/x-www-form-urlencoded"
},
transformRequest:transformRequest,
data:$scope.account,
}).then(function (response) {
xxxx
}
})
另外一點(diǎn)要注意的是
// 用于上傳application/x-www-form-urlencoded,否則一直無法提交數(shù)據(jù)
function transformRequest(obj) {
var str = [];
for(var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
}
jquery的withCredentials寫法
$.ajax({
data:data,
url:url,
type:"POST",
xhrFields:{
withCredentials:true
}
success:function(res){
...
}
})
如果不想在每個請求中寫withCredentials拉岁,可以單獨(dú)寫坷剧。
$.ajaxSetup({
xhrFields:{
withCredentials:true,
}
})