jQuery的AJAX处理401未授权 [英] jQuery ajax handle 401 Unauthorized

查看:4589
本文介绍了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.

这篇关于jQuery的AJAX处理401未授权的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆