Solved

How to capture proper error inside xmlhttprequest error handler

Posted on 2016-11-08
2
31 Views
Last Modified: 2016-11-27
HI,
I have the following code :
xhr.onerror = function() {
        callback(new Error('An error occurred while sending request xhr.status : '+xhr.statu));
    };

Open in new window


I am making a post xmlhttprequest and this error handler is invoked when say wifi is not connected.
I dont know what other cases this could also be invoked.

But the xhr.status and statusText inside is coming "" ie. blank... How do i capture proper error response what caused the xhr to fail and return a meaninful repsone message ??

Thanks
0
Comment
Question by:Rohit Bajaj
  • 2
2 Comments
 
LVL 50

Accepted Solution

by:
Steve Bink earned 500 total points (awarded by participants)
ID: 41879744
First, you're referencing xhr.statu, which is very likely to be nothing.  Correct it to xhr.status and see if that helps.

Also, you need to realize that xhr.status (assuming xhr is the return from an XMLHttpRequest) is going to hold the HTTP-level status of the request.  For successful requests, this should be 200.  If this holds something else, it could be 404 (missing resource), 403 (Forbidden), etc.  You can a full list of response codes if you're really curious.

In the context of an AJAX application, though, the status may not be what you're looking for.  It is very common to include application-level status markers or state information in a successful response's content.  For example, I could send a request to log in to an application, but send the wrong credentials.  The request status could be 200 (the page was reached successfully and returned a response), but the content of the response could be a JSON object, including properties such as status, error, etc.  If you're not seeing what you expect to see in xhr.status, start examining other properties as well.  It may be you're looking for something in the response itself.
0
 
LVL 50

Expert Comment

by:Steve Bink
ID: 41902965
Found one error in provided code.  Offered other avenues for investigation if necessary.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Capture logon name 13 73
Javascript to set controls visibility 5 36
javascript comparing two dates 3 19
Html form and modal / img src -problem 3 11
When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question