?
Solved

How to capture proper error inside xmlhttprequest error handler

Posted on 2016-11-08
2
Medium Priority
?
69 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 2000 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…
Suggested Courses

765 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