By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,871 Members | 2,418 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,871 IT Pros & Developers. It's quick & easy.

jQuery ajax error details

Frinavale
Expert Mod 5K+
P: 9,731
Hi there!

I have a function that handles all of the form submit functions on a page that uses the jQuery ajax api to submit the request asynchronously and then update a particular <div> on the page.

Here is my code:

Expand|Select|Wrap|Line Numbers
  1.     function Refresh() {
  2.         $(document.forms[0]).submit();
  3.     }
  4.  
  5.     $(document).ready(function () {
  6.         $(document).on("submit", "form", function (event) {
  7.             event.preventDefault();
  8.             var form = $(this);
  9.             $.ajax({
  10.                 type: this.method,
  11.                 url: this.action,
  12.                 data: form.serialize(),
  13.                 cache: false,
  14.                 success: function (result) {
  15.                     var lastRefreshed = new Date();
  16.                     var str = lastRefreshed.toString() + result;
  17.                     $('#itemsContent').html(str);
  18.                 },
  19.                 complete: function () {
  20.                     window.setTimeout(Refresh, 5000);
  21.                 },
  22.                 error: function (jqXHR, textStatus, errorThrown) {
  23.                     var lastRefreshed = new Date();
  24.                     var str = lastRefreshed.toString() +"<br /><br />"+errorThrown;
  25.                     $('#itemsContent').html(str);
  26.                 }
  27.             });
  28.  
  29.         });
  30.  
  31.     });
Currently, the items are refreshed every 5 seconds after a submit to the server and it works fine under normal conditions.

However, when an error occurs, I want to display the error details to the user.

To test this I stopped the server process completely to cause a "server unavailable" error.

In the error function, the textStatus parameter contains text "error" which is not very informative. I would have expected the errorThrown to contain information about the error (in this case: server not available or the HTTP status code) but it contains nothing.


Is there a way to display more information about the error to the user?

How do I retrieve this information?

Thanks,

-Frinny
Oct 23 '13 #1

✓ answered by Dormilich

Is there a way to display more information about the error to the user?
hardly when you stop the server. (or asked the other way round, if the server does not respond, where should the error message come from?)

thatís because the error messages normally come from the server itself (i.e. the 4xx and 5xx HTTP errors).

the only workaround for a stopped server I can imagine is to hook into the timeout event (if there is any, well the regular XHR seems to have a timeout event (ref.)).

for jQuery directly, you may find something in the ajaxError() description.

Share this Question
Share on Google+
3 Replies


Dormilich
Expert Mod 5K+
P: 8,639
Is there a way to display more information about the error to the user?
hardly when you stop the server. (or asked the other way round, if the server does not respond, where should the error message come from?)

thatís because the error messages normally come from the server itself (i.e. the 4xx and 5xx HTTP errors).

the only workaround for a stopped server I can imagine is to hook into the timeout event (if there is any, well the regular XHR seems to have a timeout event (ref.)).

for jQuery directly, you may find something in the ajaxError() description.
Oct 23 '13 #2

Frinavale
Expert Mod 5K+
P: 9,731
Ah, thanks Dorm :)

I should have thought about the test I was doing.

Going to try submitting to an invalid script to get a 404 error instead and test that way.

-Frinny
Oct 23 '13 #3

Frinavale
Expert Mod 5K+
P: 9,731
Yup, that's better!
It is working as expected.

Thanks :)
Oct 23 '13 #4

Post your reply

Sign in to post your reply or Sign up for a free account.