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!
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 :)
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.
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...
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.I think you should handle that ajax error with any code for your needs.
Example