XMLHttpRequest is an object in JavaScript that allows you to send AJAX requests to the server, it allows you to get a response from the server usin...
For further actions, you may consider blocking this person and/or reporting abuse
It's 2020, use promise-based
fetch()instead.I agree that you there are better solutions now. It's always good to refresh on the underlying principles and workings though
Is using
varand non-strict equality (==) a part of refreshing on the underlying principles?Using var is fine for these examples as it is block scoped. I could have use the new let/const keywords plus non-strict equality, etc, etc. But for just showing the basics of XMLHttpRequest they're not needed and it would have over complicated the simple examples.
There's no point in using legacy and problematic syntax such as
varand==. For beginners, it makes sense to show correct examples of modern JavaScript. We don't speak Old Slavonic teaching kids Russian, so why should we use the legacy syntax when teaching people how to code?The last example is even incorrect,
XMLHttpRequestwill not doJSON.stringify()automatically for you. Try running this code in the console:It sends
[object Object]instead of JSON you expect.As I will almost always wrap in
new Promiseanyway, I would suggestaxios, which isXMLHttpRequestbased and Promise based.Of course,
fetchis already a Promise.Yes I agree, axios is a brilliant little library to handlr async requests