jQuery的AJAX处理401未授权
我打电话使用jquey AJAX的第三方网页。根据他们的网页,他们给我发了状态code 200,如果登录成功,401如果登录成功。这是我的jQuery code样本。这code正常工作的IE浏览器,但不能在Chrome或Firefox的工作。可能是什么问题?
I am calling third party web page using jquey ajax. According to their page they sent me status code 200 if log-in success and 401 if log-in unsuccessful. Here is my jquery code sample. This code works fine on IE but not work on Chrome or Firefox. What could be the issue?
$.ajax({
type: 'GET',
url: hostURL + 'j_teo_security_check?callback=?',
dataType: 'json',
data: ({j_username : $("#inp_user_name").val(), j_password: $("#inp_user_pwd").val()}),
statusCode: {
401:function() { alert("401"); },
404:function() { alert("404"); },
200:function() { alert("200"); },
201:function() { alert("201"); },
202:function() { alert("202"); }
},
complete: function(httpObj, textStatus){
alert(httpObj.status);
},
error: function(){
alert("error");
},
async: false
});
我尝试了所有的功能错误,成功,完整,状态code。他们没有处理401错误。
I tried all the functions error, success, complete, and statusCode. None of them handle the 401 error.
我可以修复它为波纹管。
I could fix it as bellow.
$(document).ready(function(){
$("#cmdLogin").click(function(){
var request = $.ajax({
url : hostURL + 'j_teo_security_check',
data: ({j_username : $("#inp_user_name").val(), j_password: $("#inp_user_pwd").val()}),
dataType : "jsonp",
timeout : 5000
});
request.success(function() {
loginSuccess();
});
request.error(function(httpObj, textStatus) {
if(httpObj.status==200)
loginSuccess();
else
loginFail();
});
});
})
我所做的加入超时,因为401错误再也没有回来。因为它关系到误差函数,即使状态code 200和解析错误,我把它忽略错误内部状态200。
What I did was added timeout since 401 error never came back. Since it goes to error function even with status code 200 and parsing errors I made it to ignore status 200 inside the error.