Solved

How to capture proper error inside xmlhttprequest error handler

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

759 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now