0

Possible Duplicate:
jQuery $.get or $.post to catch page load error (eg. 404)

Maybe I am thinking this through wrong. But here goes anyway. Currently with the help of jQuery I am running $.post().

However I am posting to get contents based on a directory. Issue is the information is dynamically set. So if it turns out to be where I in a sense query for a file/folder and it doesn't exist I get a 404, 401, 500 whatever error.. Basicly in short telling me it doesn't exist. That said, how do I check for that, or catch it so when it occurs I can throw a custom error out via javascript through maybe a dialog or something.

Is it even possible? Mind you the ajax/post is not tied to a server side script currently and if it is to eventually be, it will be tied to a ruby on rails script.

Community
  • 1
  • 1
chris
  • 36,115
  • 52
  • 143
  • 252

1 Answers1

4

You can add .error(function(){...}) right after your post call to determine how to handle if your AJAX fails, and also the error code that's returned.

Code:

$.post('/path/to/script/',function(){alert('success');})
  .error(function() { alert("error"); });

Explanation:

error(jqXHR, textStatus, errorThrown)Function

A function to be called if the request fails. The function receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred. Possible values for the second argument (besides null) are "timeout", "error", "abort", and "parsererror". When an HTTP error occurs, errorThrown receives the textual portion of the HTTP status, such as "Not Found" or "Internal Server Error."

http://api.jquery.com/jQuery.ajax/

ima007
  • 467
  • 3
  • 12