I have a simple javascript that uses $.ajax() from JQuery which works great for GET/POST. However, for some users that are behind a proxy, they receive the 407 error as outlined in the post below.
407 Proxy Authentication Required
Now, as you will see, I’ve updated that post stating that the usage of JSONP will suffice as a workaround. Where I’m at now, is I don’t want to use JSONP all the time, only when it is required.
function original() {
$.ajax({
url: "http://somecool.url/foo",
data: { id:"bar"},
statusCode: {
407: foo()
},
success: function(data) {
$.each(data, function(k,v) {
$('#foo').append("<li>" + k + ":" + v + "</li>");
});
}
});
}
function foo() {
$.ajax({
url: "http://somecool.url/foo",
data: { id:"bar" },
dataType: "jsonp",
success: function(data) {
$.each(data, function(k,v) {
$('#foo').append("<li>" + k + ":" + v + "</li>");
});
}
});
}
$(document).ready(function() {
original();
});
Is it possible to persist the status of the first failure, which returns the 407 error when there is a proxy issue so that all subsequent requests do not go to the original() function and go to the foo() function?
My original answer (below) was dealing with function name override to accommodate the code in your question. However, there’s a better solution, since after all you only want to switch all requests to JSONP if and only if you receive a
407response code.$.ajaxSetup() was designed to do exactly that:
With this strategy, once
407is received, all the future AJAX requests will use JSONP.For the sake of history, here is my original answer.
You can permanently change the function stored in
originalwhen you receive a407response code for the first time:From then on, the name
originalwill refer tofoo(). You can even change the function and call its replacement at the same time: