开发者

detect a 407 error and persist for all subsequent requests

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 407 response code.

$.ajaxSetup() was designed to do exactly that:

function original() {
    $.ajax({
        url: "http://somecool.url/foo",
        data: { id:"bar"},
        statusCode: {
             407: function() {
                 $.ajaxSetup({ dataType: "jsonp" });
                 // Now all AJAX requests use JSONP, retry.
                 original();
             }
        },
        success: function(data) {
            $.each(data, function(k,v) {
                $('#foo').append("<li>" + k + ":" + v + "</li>");
            });
        }
    });
}

With this strategy, once 407 is 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 original when you receive a 407 response code for the first time:

function original() {
    $.ajax({
        url: "http://somecool.url/foo",
        data: { id:"bar"},
        statusCode: {
             407: function() {
                 window.original = foo;
                 foo();
             }
        },
        success: function(data) {
            $.each(data, function(k,v) {
                $('#foo').append("<li>" + k + ":" + v + "</li>");
            });
        }
    });
}

From then on, the name original will refer to foo(). You can even change the function and call its replacement at the same time:

407: function() {
    (window.original = foo)();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜