在之前的工作中大部分都是寫純後臺的代碼,現在到新公司需要寫部分的前端代碼,因此碰到這個問題很是難受,記錄下來;
也即是在寫POST請求時務必加上:
contentType:"application/x-www-form-urlencoded"
$.ajax({
url: u,
data: {"vCode":code},
type: "POST",
//此處必須要加上,否則POST請求到後臺時,vCode爲null;
contentType:"application/x-www-form-urlencoded",
datatype:"JSON",
async: false,
success: function (data) {
sessionCode = JSON.parse(data);
if(sessionCode.code == "success"){
//驗證碼正確則進入後臺登錄
submitlogin(loginuser, loginpwd);
}else{
$("#loginmsg").html(sessionCode.msg);
//驗證失敗之後,重新刷新驗證碼
$("#codeimg").attr("src", "/XXX/abc/def?random=" + Math.random());
return ;
}
},
error: function (XMLHttpRequest, errMes, exception) {
if (errMes == "timeout") {
} else if (errMes == "error" && XMLHttpRequest.status == 0) {
} else {
}
$("#loginmsg").html("連接服務器超時,請重試或與管理員聯繫!");
}
});