473,387 Members | 1,705 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

jQuery ajax error details

Frinavale
9,735 Expert Mod 8TB
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.

3 5951
Dormilich
8,658 Expert Mod 8TB
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
9,735 Expert Mod 8TB
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
9,735 Expert Mod 8TB
Yup, that's better!
It is working as expected.

Thanks :)
Oct 23 '13 #4

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

Similar topics

6
by: Plat | last post by:
I've Googled this for a while, to no avail. Hopefully someone can help me. Maybe I'm using the wrong terminology. Here's the scoop! Let's say I've got a simple *.ASPX page that has a syntax...
3
by: Paul Cheetham | last post by:
Hi, I have created a custom error page for my web application (c#, .Net 2) In this page I am trying to get the details of the error that has occurred so that I can display it if required. ...
7
by: Jim in Arizona | last post by:
I'm brand new at ajax. In fact, about 20 minutes ago was the first time I got it to work. The problem I'm having on another page did not work, however. I'm running into the following error: ...
4
by: koyanpaing | last post by:
Hello everyone, I have a java script problem with "jQuery("#name").editable is not a function". i open Firebug console and it shows this error. i already include jquery file also. what wrong on...
1
by: Nicodemas | last post by:
I am working with jQuery v1.6. My question is: why does the responseText property returning undefined? var AjaxResult = jQuery.ajax({ ... }); console.log(AjaxResult); // logs "Object...
1
ilya Kraft
by: ilya Kraft | last post by:
I guess it is sort of simple question, but I'm not the AJAX guy, tried it, but I still don't understand it ))) Here is the situation: 1) I get one random url from database $sql = ("SELECT...
1
by: Marknut | last post by:
I've been trying to get Google's AuthSub authentication to work for me, but to no avail. I can get the token (see below) but I cannot use the token in any way. -To get the token, the user clicks...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.