How to catch error in jQuery's load() method

M Aung picture M Aung · Apr 15, 2009 · Viewed 52.4k times · Source

I'm using jQuery's .load() method to retrieve some data when a user clicks on a button.

After the load successfully finishes, I show the result in a <div>.

The problem is, sometimes an error occurs in load() while retrieving the data.

How can I catch an error in load()?

Answer

cgp picture cgp · Apr 15, 2009

load() documentation.

Just a little background on how a load error happens...

$("body").load("/someotherpath/feedsx.pxhp", {limit: 25}, 
    function (responseText, textStatus, req) {
        if (textStatus == "error") {
          return "oh noes!!!!";
        }
});

Edit: Added a path other than the root path as requested by comments.