Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking Jquery ajax load success

Tags:

jquery

ajax

I've been using this to load another page, and just using their example :

$("#links").load("/Main_Page #jq-p-Getting-Started li");

However I want to run a function when the load is successful, and a different one if it fails, unfortunately the description on the callback says:

The function called when the ajax request is complete (not necessarily success).

How do I make sure the load was successful?

Thanks.

like image 674
Mark Avatar asked Jun 01 '09 19:06

Mark


2 Answers

Check the textStatus of the response.

$("#links").load("/Main_Page #jq-p-Getting-Started li", 
  function (responseText, textStatus, XMLHttpRequest) {
    if (textStatus == "success") {
         // all good!
    }
    if (textStatus == "error") {
         // oh noes!
    }
  }

In determination to figure out the issue:

A JSBin example of success and fail

like image 165
cgp Avatar answered Sep 25 '22 15:09

cgp


You might try using the more detailed $.ajax() method call. This will give you more fine-grained control over the AJAX call (but it also requires a bit more work than a simple .load().) It will allow you to call different functions based on whether your call returns success or error.

Check out: http://docs.jquery.com/Ajax/jQuery.ajax#options for documentation on this function. If you click the "Options" tab on that page, you will get a full listing of the parameters that you can pass to the $.ajax() function.

like image 28
Ender Avatar answered Sep 22 '22 15:09

Ender