Hello everyone! While working on an interesting project, I had an idea to tell about the difference between fetch and XMLHTTPRequest. In fact, the question sounds quite interesting if you know the history of its creation, in the sense that it is a little incorrect, but you still need to know about it, because, let's say, for me as a developer, when I deal with all sorts of APIs every day, it is useful to know so as not to create unnecessary code.
What is XMLHTTPRequest and a brief history of its creation
In javascript, XMLHttpRequest objects are used to interact with servers. You can retrieve data from a URL without having to do a full page refresh. This enables a Web page to update just part of a page without disrupting what the user is doing. But generally speaking, to put it bluntly, it is an API. And when we hear this, we immediately remember the concepts of event loop, asynchronous (when the code execution will be completed after a certain time), etc. For example, code using XMLHTTPRequest looks like this:
const xhr = new XMLHttpRequest(); // method, URL, [isAsync, user, password] xhr.open("GET", "/api/getPage"); // body xhr.send()
The history of the creation of XMLHTTPRequest itself takes us back to the distant years when dinosaurs walked... Okay, just kidding. In fact, this API was first developed by Microsoft and was introduced in the Outlook on the web component of the Microsoft Exchange Server software 2000 product.
Back then, it was called differently - IXMLHTTPRequest and was a little, if one can say so in the first versions of the interface, different from what it is now. That is, the base obviously remained, but it is obvious that adjustments were made over 25 years. Later, it was added to MSXML 2.0, and even later, to Internet Explorer 5 in March 1999.
Then, Mozilla programmers developed their own version of API based on IXMLHTTPRequest, then called nsIXMLHttpRequest, and made it accessible through our beloved XMLHttpRequest. All this was added to Gecko version 0.6 in December 2000 (Gecko is a browser engine used in FireFox and many other places). At that time, there was no such thing as FireFox, because its first version was released in September 2002 and was called Phoenix, then FireBird and only by 2004 FireFox. Then, before all this, there was Netscape Navigator, but the confrontation with Internet Explorer and that's it - that's another story, which there is no point in writing in this article. Later, support was added to Safari, Opera and other browsers. So when people tell you that this object is used for compatibility with old browsers, then of course they are right, because it takes us to the late 90s, when web development was just developing. You can even remember the time of the dotcom bubble, but, admittedly, it was a little earlier, or at that time (if we take Internet Explorer), but anyway it was then.
So what's the difference?
So, returning to the question about the difference, the most important difference is already hidden in history, because XMLHTTPRequest is just an old, but supported API, and fetch is just a new API, which kind of replaces it. As of today, XMLHTTPRequest 2.0 was released in January 2012, but the latest edition of 1.0 standard was released in December of the same year. This is the current standard for this object. But now imagine the faces of the developers who wrote this all the time:
const xhr = new XMLHttpRequest(); // method, URL, [isAsync, user, password] xhr.open("GET", "/api/getPage"); // body xhr.send()
instead of this
var xhr = new XMLHttpRequest(); xhr.open('GET', '/api/getData', true); xhr.onload = function () { if (xhr.status >= 200 && xhr.status <p>a few years ago. That's the only thing that comes to mind right away.</p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/173164813124292.jpg?x-oss-process=image/resize,p_40" class="lazy" alt="✅What is the difference between fetch and XMLHTTPRequest?"></p> <p>But seriously, here is a short list of differences:</p> <ol> <li><p>Promise-oriented: The Fetch API uses promises, which makes it easier to work with asynchronous code and allows you to use convenient constructs like .then() and async and await.</p></li> <li><p>Simplified interface: The Fetch API has a simpler and more intuitive syntax. For example, to make requests, you don’t need to create an object and set its properties, as is the case with XHR.</p></li> <li><p>Support for new features: The Fetch API supports new features such as Request and Response objects, which provide convenient methods for working with requests, responses, and their transformations.</p></li> <li><p>Support for streams: The Fetch API supports streaming data, which allows you to work with large amounts of data as it is received.</p></li> <li><p>Canceling requests: Although the Fetch API does not have built-in support for canceling requests, you can use the abort controller to do so, which makes managing requests more flexible.</p></li> <li><p>Improved customization: The Fetch API provides more options for configuring requests, such as setting headers, caching modes, and other parameters.</p></li> <li><p>cors support: The Fetch API provides more flexibility in working with CORS (Cross-Origin Resource Sharing), which allows for more flexible configuration of requests to resources from other domains.</p></li> <li><p>Better error handling: When working with the Fetch API, you can better handle response statuses, since a request error (e.g. network unavailable) will reject the promise, while a successful response with a 4xx or 5xx code will not reject it, and you will need to check the status code yourself.</p></li> </ol> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/173164813671282.jpg?x-oss-process=image/resize,p_40" class="lazy" alt="✅What is the difference between fetch and XMLHTTPRequest?"><br> <em>This is of course not me, but it's just a cool picture</em></p><p>And, of course, there are many more differences, but I don’t see the point in listing them now, because by all parameters it is clear that this is simply an improved version of the old standard for modern browser requirements.</p> <h2> Afterword </h2> <p>At first I thought to write one line that fetch is a newer standard, but it would be too simple and uninformative, when in fact there is a lot of cool stuff there that could be told. You can write a whole article about fetch alone, but there is no particular point in this article. But, overall, in my opinion, everything seems ok.</p> <p>Thank you very much for reading the article!</p> <h2> P. S. </h2> <p>By the way, this project was HMPL. It is a small template language for displaying UI from server to client. It would be great if you could rate this project with a star. Thanks!</p> <p>Star HMPL </p>
The above is the detailed content of ✅What is the difference between fetch and XMLHTTPRequest?. For more information, please follow other related articles on the PHP Chinese website!

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

The power of the JavaScript framework lies in simplifying development, improving user experience and application performance. When choosing a framework, consider: 1. Project size and complexity, 2. Team experience, 3. Ecosystem and community support.

Introduction I know you may find it strange, what exactly does JavaScript, C and browser have to do? They seem to be unrelated, but in fact, they play a very important role in modern web development. Today we will discuss the close connection between these three. Through this article, you will learn how JavaScript runs in the browser, the role of C in the browser engine, and how they work together to drive rendering and interaction of web pages. We all know the relationship between JavaScript and browser. JavaScript is the core language of front-end development. It runs directly in the browser, making web pages vivid and interesting. Have you ever wondered why JavaScr


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Atom editor mac version download
The most popular open source editor

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.
