Solved

How to capture proper error inside xmlhttprequest error handler

Posted on 2016-11-08
2
59 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 51

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 51

Expert Comment

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

Featured Post

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

691 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