5

I know my title isn't very clear.

For example, this is the structure of my code:

if (foo == something) {
  // Ajax call 1, let it be $.ajax("foo1.html") and so on
} else {
  // Ajax call 2, let it be $.ajax("foo2.html") and so on
}

How would I test if $.ajax("foo1.html") has actually been run?

Please don't tell me to test if foo == something again. My actual code is much, much more complicated, so please answer the question from the view of the ajax call.

Is this possible at all?

5 Answers 5

13

I'm not sure if I understood you right, but jQuery will mix a Deferred object into its jXHR object and you can just check its state.

var resp = $.ajax({});

// somewhere else...
if( resp.state() === 'resolved' ) {
}

Other states are rejected and pending, see http://api.jquery.com/deferred.state/

Of course, you can get all advantages of those Deferred objects aswell, like adding more event handlers for certain things afterwards (.done(), .fail(), etc) or just wait for the promise to fullfil using $.when().

Sign up to request clarification or add additional context in comments.

3 Comments

yep, exactly what I really wanted.
is checking state() is like checking if the status of resp is 200?
I didn't work with jQuery for quite a while now, but I'm pretty sure you cannot rely on the status code 200 exactly. The promise will also resolve on status like 304 (cached) and maybe some others. But you can be certain, that the request finished successfully either way.
6

You can set a callback in your ajax call:

$.ajax({
  url: "foo1.html"
}).done(function() { 
  alert('Done!');
});

1 Comment

well, not exactly what I meant. But still reasonable anyway.
5

I would set variable before AJAX call and reset it in the success callback like that :

var isRunning = true;

$.ajax({
    url: url,
    success: function(resp){
      isRunning = false;
    }
});

Comments

1

I had similar issues. I used this code:

var isRunning = false; // whether ajax call is running
if(isRunning==false){
   isRunning = true;
   $.ajax({
     // do your stuff
     // make sure you set
     isRunning = false;
     // on success
   });
}

Comments

0

Wrap the call in a try catch.

if the call to 'foo1' fails because the method does not exist on the server, in the catch call foo two and then three all the way down until you have exhausted all your fall backs.

If you know the method exists and think it will fail, then set a status on it that can be returned if the server fails then handle the return in the ajax callback.

5 Comments

jQuery does not throw exceptions on failed Ajax requests.
OK, but checking for a function's existance shouldn't be done with try-catch
that is fair, but if you are in a state where you think the function should exist but there is a chance it does not, a try catch will make sure you don't break down.
No, you should should check the typeof possibleFunction instead of blindly calling it, try-catch is too much execution overhead
How do you typeOf a page that is going to be called by ajax? wouldn't it just return that the $.ajax() function exists?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.