3

I am trying to make AJAX calls using the jQuery .load() method. When the request passes, it loads the return data correctly. If I get a 500 error, it does not. Is there a way to output the failed request message information as well?

$("#activity").load("/forumsetup", { id:myid }, 
       function(data) {
          $("#restart").css("visibility","visible");
        });  

I can see it in firebug, but would like to load it onto my page.

JiminyCricket
  • 7,050
  • 7
  • 42
  • 59
  • http://stackoverflow.com/questions/753300/how-to-catch-error-in-jquerys-load-method Do the answers in this question solve it? – Matt May 17 '11 at 14:54

2 Answers2

12

From jQuery doc page: http://api.jquery.com/load/

$("#success").load("/not-here.php", function(response, status, xhr) {
  if (status == "error") {
    var msg = "Sorry but there was an error: ";
    $("#error").html(msg + xhr.status + " " + xhr.statusText);
  }
});
Alxandr
  • 12,345
  • 10
  • 59
  • 95
  • Nice, that did it. A classic case of RTFM on my part. – JiminyCricket May 17 '11 at 15:00
  • Hehe. Well, I remember reading jQuery adding support to detect failed request on all of their request-functions, so it was just a quick lookup. Though, if you are content with the answer, please mark it as the answer. – Alxandr May 17 '11 at 15:03
0

Yes, jquery returns an object in xhr. You only have to access it in the following way:

$("#success").load("/not-here.php", function(response, status, xhr) {
if ( status == "error" ) 
{
 if (xhr.status == 500 )
  do something...

for more information, check: http://api.jquery.com/jQuery.ajax/#jqXHR

Sergio Roldan
  • 59
  • 1
  • 5