jQuery ajax handle 401 Unauthorized

Pra*_*sha 24 ajax jquery jquery-mobile

我使用jQuery ajax调用第三方网页.根据他们的页面,他们在登录成功时发送状态代码200,如果登录失败则发送401.这是我的jquery代码示例.此代码在IE上运行正常,但在Chrome或Firefox上无效.可能是什么问题?

$.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
});
Run Code Online (Sandbox Code Playgroud)

我尝试了所有函数错误,成功,完成和statusCode.他们都没有处理401错误.

Pra*_*sha 10

我可以把它当作吼叫.

$(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();
            });
        });
    })
Run Code Online (Sandbox Code Playgroud)

我所做的是添加超时,因为401错误永远不会回来.由于它甚至在状态码200和解析错误时都进入错误功能,因此我忽略了错误中的状态200.