Solved

How to capture proper error inside xmlhttprequest error handler

Posted on 2016-11-08
2
40 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
jQuery Toggle & Anchor Links 5 43
Override MS CSS in SharePoint Online Modal Form 19 43
asp Google Map 2 28
JQuery Syntax... 4 33
Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…

856 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