how to fool jqXHR to succeed always

2020-04-11 10:51发布

I'm trying to make jQuery's ajax calls always return as if they succeeded, e.g. even when I don't have network, I will return some locally saved data

It that possible?

I tried using the $.ajaxPrefilter and calling the success function of the jqXHR, but still it won't behave as if the request has finished :(

Thanks!

标签: jquery
4条回答
够拽才男人
2楼-- · 2020-04-11 11:29

If im understanding you correctly, you want to make the jqXHR act like it succeeded, when it actually failed?

If so, .pipe is your friend :)

var jqDeferred = $.ajax(...something something);

//make the deferred always resolve as success, calling all success callbacks on original.
//AFAIK, dosent pass any arguments to the original success callbacks.
jqDeferred.pipe( null, function(args){ return $.Deferred().resolve(); });

//same as above, but try to pass all arguments through to the success callbacks.
jqDeferred.pipe( null, function(args){ return $.Deferred().resolve.apply(this, arguments); });

I wanted to do this recently and couldnt find any easy instructions, hope it helps. Im not super sure about the argument passing, as I have only used the first form in anger - we didn't need any arguments passed to our success callbacks.

Pipe is wicked.

查看更多
对你真心纯属浪费
3楼-- · 2020-04-11 11:41

Well, a quick update: non of the solutions above worked.
What I eventually had to do to walk around this problem, is replace jQuery's ajax function with my implementation, that checks if there is network connection.
if there is, I proxy the request back to the original ajax function, else, I create the same return structure as ajax's would and return it

Again, I hate answering my own questions, especially when it's not really an answer but more of a workaround. sad...

查看更多
对你真心纯属浪费
4楼-- · 2020-04-11 11:46

You can use .ajaxSetup with your own custom beforeSend handler. In your handler you can check if the url is accesible, if it's not you can cancel the request and emulate a call to success with your own data.

查看更多
Fickle 薄情
5楼-- · 2020-04-11 11:51

I think you should handle that ajax error with any code for your needs.

error(jqXHR, textStatus, errorThrown)

Function A function to be called if the request fails. The function receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred. Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror". When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error." As of jQuery 1.5, the error setting can accept an array of functions. Each function will be called in turn. Note: This handler is not called for cross-domain script and JSONP requests. This is an Ajax Event.

Example

 error: function (xhr, status, error) {
            var err = eval("(" + xhr.responseText + ")");
            if (err.Message == 'SomeMessage') {
               //Return Your data, handle this error...
            }
        }
查看更多
登录 后发表回答