3

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!

Nadav
  • 1,167
  • 2
  • 19
  • 43
  • 4
    Why do you want to do it that way? If you want to return local data as a fallback, why not do the natural thing and utilize the error handler to activate the fallback? – Jon Jun 15 '11 at 08:45
  • I want that behavior so when i return local data on the fallback, I want this to be transparent, as if the request succeeded – Nadav Jun 15 '11 at 08:58

4 Answers4

3

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.

dalyons
  • 1,304
  • 3
  • 15
  • 23
  • As of jQuery 1.8, pipe is deprecated. See write-up in [jQuery Deferred then](https://api.jquery.com/deferred.then/). – Griffin May 13 '15 at 15:38
0

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...
            }
        }
nemke
  • 2,440
  • 3
  • 37
  • 57
  • I have a way of finding if I have access to the url of the request without waiting for it to timeout, and I don't want the user to wait until timeout – Nadav Jun 15 '11 at 09:01
  • You could set timeout to 0 if you know that your url is not accessible. Try that if it will work... – nemke Jun 15 '11 at 09:04
0

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.

Variant
  • 17,279
  • 4
  • 40
  • 65
  • thanks, my question is how to emulate that success call. can you please provide an example? – Nadav Jun 15 '11 at 10:31
  • I'll try to get a working fiddle when I'll have some time soon – Variant Jun 15 '11 at 10:40
  • For now I guess you can retrieve the handler for `success` from the settings object you get with the `beforeSend` handler. – Variant Jun 15 '11 at 10:41
  • as I said, I did call the success func. but appears that calling it alone does not produce the same affect as a successful ajax call (i still get timeout for one...) – Nadav Jun 15 '11 at 10:52
  • What do you mean ? did you cancel the original XHR? – Variant Jun 15 '11 at 10:56
-1

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

Nadav
  • 1,167
  • 2
  • 19
  • 43