Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to fool jqXHR to succeed always

Tags:

jquery

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!

like image 369
Nadav Avatar asked Jan 18 '26 00:01

Nadav


1 Answers

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.

like image 134
dalyons Avatar answered Jan 19 '26 15:01

dalyons



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!