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.
Can you add a hidden field onto the page (i'm guessing its dynamically generated) like:
<input type="hidden" name="isLoggedIn" value="true" (or false) />
Then in you javascript, place
if ($('input[name=isLoggedIn"]').val() == "true")
{
//your ajax call here
}
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: