I’m using the following code to have multiple ajax requests like this:
request 1 start | request 1 finish | request 2 start | request 2 finish | …
This is the code:
var startingpoint = fireRequest(1);
$.each(types,function(ix,type)
{
startingpoint = startingpoint.pipe( function()
{
alert(startingpoint.status); // undefined
return fireRequest(type);
});
});
fireRequest is just a switchcase to the correct ajax function which returns $.ajax(…)
I’d like the chain to stop when one request fails. I started implementing it and as a test I’d like to alert the status of the ajax object, but it shows ‘undefined’. How can I get the status?
The behavior you are trying to implement is already the behavior of the .pipe() method. It takes two callbacks as parameters and will ONLY execute the done callback and continue along the chain if the previous request succeeds. This can be illustrated in the following jsfiddle:
http://jsfiddle.net/dflor003/Vq2YF/ (Note: open this in a browser that has built-in JSON.stringify support and console.log support)
If you do want to check the status of a request, it will take the status as the second parameter to the done callback. More detail can be found on jQuery’s API documentation site: http://api.jquery.com/deferred.pipe/