javascript - 401 未经授权的问题

标签 javascript jquery ajax

我正在使用jQuery.get()方法

$.get('login.php', function(d, textStatus, jqXHR){
    //alert(jqXHR.status)
    var status = jqXHR.status; 
    if((status==200)||(status==202)){
        window.location.href = 'dashboard.html';
    }else if(status==401){
        alert('Error in login details')
    }else{
        alert('Unknown Error')
    }
});

工作正常。当 200 和 202 时,它将重定向到仪表板页面。但除了 200 和 202 之外,它会在控制台中传递错误,但不会显示警报。

最佳答案

您需要为 fail 状态添加一些事件处理程序,这将处理 4xx 和 5xx 错误。 success 状态仅处理指示请求成功的 HTTP 代码。来自 http://api.jquery.com/jQuery.get

var jqxhr = $.get( "example.php", function(data, status) {
  alert( "success - " + status );
})
.done(function(data, status) {
  alert( "second success - " + status );
})
.fail(function(data, status) {
  alert( "error - " + status );
})
.always(function(data, status) {
  alert( "finished - " + status );
});

关于javascript - 401 未经授权的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25662412/

相关文章:

javascript - 使用复选框增加 Bootstrap 进度条

javascript - 如何禁用 HTML 链接

php - 如何在 jquery 中创建关联数组并通过 ajax 发送它以使用 php 进行解析?

javascript - 开 BLUR 不开火

javascript - document.getElementById ("id").value 正在工作,但 $('#id).val 不起作用

javascript - 排名数字数组值

javascript - Jasmine spy jquery

jquery - 使用 jQuery 从 HTML 文本查询脚本元素

javascript - 如何将对象传递给jquery回调?

javascript - 如何 npm 安装并强制安装最新版本(永久)?