\$\begingroup\$
\$\endgroup\$
2
The intention is to implement an xmlHttpRequest in plain vanilla js while considering all possible errors and problem situations without crashing in the browser. Result and faults are to be properly communicated back to the caller.
function get() {
return new Promise(function(resolve, reject){
var xmlHttpRequest = new XMLHttpRequest();
xmlHttpRequest.open("GET", "http://localhost/products", true);
xmlHttpRequest.onreadystatechange = function() {
if (xmlHttpRequest.readyState === 4) {
try {
var data = JSON.parse(xmlHttpRequest.response);
} catch (error) { // in case parser error
reject(error);
return;
}
if(xmlHttpRequest.status === 200) {
resolve(data);
} else {
reject(data);
}
}
};
xmlHttpRequest.addEventListener("error", function(error){
reject(error);
});
xmlHttpRequest.send();
});
}
Simon Forsberg
59.7k9 gold badges157 silver badges311 bronze badges
asked Feb 22, 2019 at 4:30
-
\$\begingroup\$ I'd also like to mention that there's a status 0 condition, it happens when browser is offline, that means no json response etc. \$\endgroup\$Developer– Developer2019年02月22日 04:36:19 +00:00Commented Feb 22, 2019 at 4:36
-
\$\begingroup\$ Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers. \$\endgroup\$Simon Forsberg– Simon Forsberg2019年02月25日 08:58:59 +00:00Commented Feb 25, 2019 at 8:58
1 Answer 1
\$\begingroup\$
\$\endgroup\$
6
Here are a few issues with your code
- If older browsers aren't a concern, you can just use
fetch
instead of XHR. It's built-in and uses promises. - HTTP 200 isn't the only "successful" response status. See https://en.wikipedia.org/wiki/List_of_HTTP_status_codes.
- Status "0" is a generic network error, not just being offline. It can also be caused by the browser suppressing the request due to some restriction, an extension blocking the request, an insecure connection/invalid certificate.
- Send an
Accept
header withapplication/json
as value. This is because, while the server might support JSON, it might not respond with it by default. - The third argument of
xhr.open
is by defaulttrue
. You may omit that third argument. - You can use
readystatechange
withaddEventListener
instead of theonreadystatechange
property to assign your callback.
answered Feb 22, 2019 at 17:40
-
\$\begingroup\$ Thanks Joseph for review, I've edited with version 2 of my code. It'd be very helpful to know the exact error message for your point #3, is there a property that I can read on xhr. REST contract dictates 200 for the success in this case. I've incorporated the rest of the feedback. please review. \$\endgroup\$Developer– Developer2019年02月25日 06:10:09 +00:00Commented Feb 25, 2019 at 6:10
-
\$\begingroup\$ Hi Joseph, they didn't let me update the code, can you please let me know how to get specific error details when status is 0 from xhr object? \$\endgroup\$Developer– Developer2019年02月25日 09:01:01 +00:00Commented Feb 25, 2019 at 9:01
-
\$\begingroup\$ hi joseoph, reaching out to you again, can you please explain how to read such kind of errors from xhr object, I've tried statusText but it's empty string. \$\endgroup\$Developer– Developer2019年03月01日 00:23:13 +00:00Commented Mar 1, 2019 at 0:23
-
\$\begingroup\$ @Developer Status 0, as far as I know, does not provide any textual response. This behavior may be vendor-specific, but I wouldn't count on it to return anything meaningful cross-browser. Hence "generic network error". \$\endgroup\$Joseph– Joseph2019年03月01日 13:25:09 +00:00Commented Mar 1, 2019 at 13:25
-
\$\begingroup\$ Thanks Joseph. I've searched a lot and I'm totally blocked on this. So there's no description on xhr, that's disappointing that API is designed like this. In other words it's an error with no details at the code level, and I'll be forced to open developer tools and see what went wrong. (it's harder on mobile apps) \$\endgroup\$Developer– Developer2019年03月01日 13:29:10 +00:00Commented Mar 1, 2019 at 13:29
default