Ajax Battle: XMLHttpRequest vs. the Fetch API
blog.openreplay.com
blog.openreplay.com
No:
The XMLHttpRequest.withCredentials property is a boolean value that indicates whether or not cross-site Access-Control requests should be made using credentials such as cookies, authorization headers or TLS client certificates.
https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequ...
XMLHttpRequest always sends browser cookies for same-origin requests. They are not included for cross-origin requests unless explicitly requested. Fetch does not send cookies by default for either type of request, unless explicitly requested.
Also note that the server must use the Access-Control-Allow-Credentials header for the response to be made available to the client code when making a cross-origin request with credentials.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Ac...
It’s such a strange take in the coding world. I don’t know why other developers get to decide what is clean, simple, easier to understand, etc, when it comes to my code or the way I write code.
I’ve been using xmlhttprequest for YEARS and honestly, it’s pretty dang simple. It’s pretty darn clean. And when we see it, we know what it’s doing and how it works.
I’m not sure of the motivation behind changes and new API’s like this that keep trying to fix what really isn’t broken for most uses.
Please explain the latter bit again.
I too see no need to switch suddenly to this fetch api when older browser support is important to my daily driver web app. I found the tone of this article condescending when browser apis have mega committee-ware battlescars all over them and likely implement both of xyz with the same call to epoll anyway, (joke about systems programming)
if (xhr.readyState !== 4) return;
is gross and stateful. Certainly you can learn it and use it but .then((res) => res.json())
is much more declarative.Not saying XMLHttpRequest API's is brilliant, but fetch's API also doesn't exactly fill me with joy.
These strange notions of burden and beauty…
IE, this is why async / await was added to JavaScript: The code is significantly easier to read (cleaner) than promises.
They're all missing the point, frankly. The point is, we're stuck in this vicious cycle where we all get selective amnesia and forget why someone was invented, and we decide that it's old/ugly/unergonomic/ungraceful/unclean/unsimple/etc./etc. and invent something new. Woe to them that love the fetch API, and fall into a time warp to 2005 when everyone was singing the praises of "AJAX". You'd be bowled over by the sheer culture shock. (Likewise, I can't wait for 2035, when we'll have invented some emoji-based way to represent fetches, and those still using fetch() or axios() will be seen as hipsters/clueless).
[1] https://github.com/axios/axios [2] https://blog.logrocket.com/fetch-api-node-js/
https://github.com/magcius/noclip.website/blob/master/src/Da...
with(console){
with(new XMLHttpRequest()){
onload=()=>log(responseText);
onerror=error;
open("GET", "https://..");
send();
}
}
I'm only half joking. console.log(await fetch("https:/...").then(r => r.text()))
I agree progress reporting needs improvement though.Axios has many nice features (such as interceptors) and I think is just better than fetch, other than the fact that you need to add a few KB to your bundle of course.
<a download href="example.com/my/download/route"></a>
and that always worked well for me. No idea if that would be helpful at all, I'm operating at a very surface level knowledge here.If this works, why isn't this documented in an obvious manner on something like MDN?
Only problem is files.
I'm serving video files locally, and for some reason the server stops returning file requests at some point.
I was told to use a real webserver, but it doesn't seem trivial, at least on windows.
For instance, this means you can do an XMLHttpRequest in your app's router, and decouple your presentation from dealing with endpoints. Fetch can't do this.
Your router should not block, and the UI on your next page should show a spinner or other loading indicator while it runs a fetch (or XmlHTTPRequest)
You should be able to mock fetch/xmlhttprequest at any point, not just in your router. You should be able to mock those calls without needing to block the UI thread at runtime.
Promises are also specced so that their handlers don't run until the event loop completes a turn, which means that no matter what there is now an opportunity for queued callbacks to run before you actually process the response to your request, even if it hit in-memory cache.