获得浏览器控制台摆脱401 Ajax错误的 [英] Get rid of a 401 (unauthorized) ajax error in browser console

查看:2224
本文介绍了获得浏览器控制台摆脱401 Ajax错误的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打电话是通过JavaScript使用 jQuery.ajax 调用的API。该API 401响应,如果用户没有通过验证,我想忽略这个错误仅此呼叫。

I'm calling an api through javascript using a jQuery.ajax call. The api respond with 401 if the user is not authenticated and I want to ignore this error only for this call.

我试过在jQuery的选项描述的所有回调选项,但错误仍然在浏览器控制台弹出(无论是在Firefox和Chrome)。

I've tried all the callback options described in the jQuery options but the error still pops out in the browser console (both in Firefox and Chrome).

下面是一个简单的code这触发错误:

Here is a sample code which trigger the error:

$.ajax({
    type: 'GET',
    url: '/url-with-auth',
    dataType: 'json',
    statusCode: {
        401: function (error) {
            // simply ignore this error
            console.log(error);
        }
    },
    complete: logall,
    error: function () {
        // simply ignore this error
        console.log(error);
    },
    async: false
}).fail(function () {
        // ignore the error and do nothing else
        console.log("$.get failed!");
    });

这也有,因为浏览器认为该用户没有通过验证,所以在以下请求,用户需要重新输入自己的HTTP请求的凭据再次它是由.htaccess文件保护的暂存计算机上的用户凭据的副作用

This also has the side effect of requesting again the user credentials on a staging machine which is protected by an .htaccess file since the browser thinks that the user is not authenticated, so in following requests the user needs to reenter his http credentials.

有没有办法完全忽略这个错误?

Is there any way to completely ignore this error?

目前我有一个完全缓存应用程序端的HTML模板,我想保持这种行为表现的原因。这改变了布局的唯一部分是当前用户的登录信息,所以我渲染(缓存)页面,登录用户不预设标记,然后使用jQuery我更换标记的相关要素与登录信息通过一个Ajax调用返回。因此,如果用户没有登录和AJAX端点返回401我什么都不需要,标记应保持不变。

Currently I have an HTML template which is entirely cached application side and I want to keep this behavior for performance reasons. The only part which changes in the layout is the login informations for the current user, so I'm rendering the (cached) page with the default markup for not logged in users and then using jQuery I replace the relevant elements of the markup with the login informations returned by an ajax call. So if the user is not logged and the ajax endpoint return 401 I don't need anything, the markup should remain the same.

一切工作在这一刻,只有丑陋的事情是,我有401,我想的JavaScript错误控制台,并摆脱它。

Everything is working at this moment, the only ugly thing is that I have that 401 javascript error in the console and I'd like to get rid of it.

下面是我在谈论的消息的截图:

Here is a screenshot of the message I'm talking about:

推荐答案

我也遇到了这个问题,并希望河畔preSS在JavaScript控制台中的例外HTTP 401和404等错误。我发现这个问题,不明白为什么错误()和状态code()处理没有安静异常,所以我做了我自己挖进去了。

I also ran into this issue and wanted to surpress the exception in the JavaScript console for HTTP 401 and 404 etc errors. I found this question and didn't see why the error() and statusCode() handlers didn't quiet the exception so i did my own digging into it.

据我可以告诉它原来,这是一个在浏览器原生的AJAX XMLHtt prequest对象,而不是使用jQuery的问题。为了证明这一点,我用了以下测试code:

As far as i can tell it turns out that this is an issue with the native ajax XMLHttpRequest Object in the browser, not with jQuery. To prove this i used the following test code:

function xhrTest(index) {
    var ajaxObjects = [
        $.ajaxSettings.xhr(), // what jquery returns
        new XMLHttpRequest()  // the native one in the browser
    ];
    var ajax = ajaxObjects[index];
    ajax.onreadystatechange = function() {console.log("xhrTest: state: " + ajax.readyState)};
    var errorHandler = function() {console.log("xhrTest: error");};
    ajax.onerror = errorHandler;
    ajax.onabort = errorHandler;
    ajax.open("GET", "/fileThatDoesNotExist", true);
    ajax.send();    
}

请注意,与调用这个时候(我做到了从JavaScript控制台)或者xhrTest(0)或xhrTest(1),它绕过了jQuery,该结果是一样的:

Note that when calling this (i did it from the JavaScript console) with either xhrTest(0) or xhrTest(1), which bypasses jQuery, that the results are the same:

这篇关于获得浏览器控制台摆脱401 Ajax错误的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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