ajax漏洞 console_获得浏览器控制台摆脱401 Ajax错误的

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.

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).

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!");

});

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?

Edit

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.

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:

解决方案

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.

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();

}

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:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值