0 added
0 removed
Original
2026-01-01
Modified
2026-02-21
1
<p>(<a>https://hexlet.io/courses/http_protocol</a>) (<a>https://hexlet.io/courses/http-api</a>)</p>
1
<p>(<a>https://hexlet.io/courses/http_protocol</a>) (<a>https://hexlet.io/courses/http-api</a>)</p>
2
<p>Using a DOM tree does help make our sites more lively. But it is still not enough to create standalone widgets or full-fledged<a>Single Page Application</a>(SPA) with a backend.</p>
2
<p>Using a DOM tree does help make our sites more lively. But it is still not enough to create standalone widgets or full-fledged<a>Single Page Application</a>(SPA) with a backend.</p>
3
<p>Let s take a specific example. Many services allow using different widgets, such as weather or currency rates. It works like this: you receive some code from a service and insert it into your HTML. Next, this code loads the widget itself and periodically requests the necessary data from the service servers.</p>
3
<p>Let s take a specific example. Many services allow using different widgets, such as weather or currency rates. It works like this: you receive some code from a service and insert it into your HTML. Next, this code loads the widget itself and periodically requests the necessary data from the service servers.</p>
4
<p>It can happen whenever the widget user clicks buttons that require new data, such as to show the weather for the next week.</p>
4
<p>It can happen whenever the widget user clicks buttons that require new data, such as to show the weather for the next week.</p>
5
<p>We use a similar widget on Hexlet; you can see it in the lower-right corner of every page. It lets you search through our guide and message our support. The widget does not interact with the backend of Hexlet because it works using a separate service.</p>
5
<p>We use a similar widget on Hexlet; you can see it in the lower-right corner of every page. It lets you search through our guide and message our support. The widget does not interact with the backend of Hexlet because it works using a separate service.</p>
6
<p>The key technology here is a mechanism for executing HTTP requests directly from the browser. We call it<strong>AJAX</strong>, which stands for<em>Asynchronous JavaScript and XML</em>. Despite the name, this technology works with other things, not just XML.</p>
6
<p>The key technology here is a mechanism for executing HTTP requests directly from the browser. We call it<strong>AJAX</strong>, which stands for<em>Asynchronous JavaScript and XML</em>. Despite the name, this technology works with other things, not just XML.</p>
7
<h2>XMLHttpRequest</h2>
7
<h2>XMLHttpRequest</h2>
8
<p>Before the html5, browsers provided an XMLHttpRequest object:</p>
8
<p>Before the html5, browsers provided an XMLHttpRequest object:</p>
9
<p>It's inconvenient, so everyone uses a wrapper from the jQuery library. There'll be more about this in our lesson dedicated to jQuery.</p>
9
<p>It's inconvenient, so everyone uses a wrapper from the jQuery library. There'll be more about this in our lesson dedicated to jQuery.</p>
10
<h2>Fetch</h2>
10
<h2>Fetch</h2>
11
<p>With the HTML5 standard, a new mechanism for HTTP requests appeared:</p>
11
<p>With the HTML5 standard, a new mechanism for HTTP requests appeared:</p>
12
<p>As you can see, fetch is a function that returns a promise. It's convenient and pleasant to work with it. And thanks to the existence of<a>polyfills</a>, you don't have to worry about a browser not supporting this mechanism.</p>
12
<p>As you can see, fetch is a function that returns a promise. It's convenient and pleasant to work with it. And thanks to the existence of<a>polyfills</a>, you don't have to worry about a browser not supporting this mechanism.</p>
13
<p>Note that response.json also returns a promise. In addition to json, data can be obtained using the blob, text, formData, and arrayBuffer functions.</p>
13
<p>Note that response.json also returns a promise. In addition to json, data can be obtained using the blob, text, formData, and arrayBuffer functions.</p>
14
<p>Sending a form with a POST request:</p>
14
<p>Sending a form with a POST request:</p>
15
<p>Sending the form as JSON:</p>
15
<p>Sending the form as JSON:</p>
16
<p>For all its advantages, fetch is a low-level mechanism. For example, when working with JSON, you'll have to set headers manually and do various things with data that otherwise could be automated.</p>
16
<p>For all its advantages, fetch is a low-level mechanism. For example, when working with JSON, you'll have to set headers manually and do various things with data that otherwise could be automated.</p>
17
<p>That's why programmers created several libraries that work similarly but provide many more features. Moreover, many of these libraries are isomorphic, so they work the same way both in the browser and on the server. One of the most popular libraries is<a>axios</a>.</p>
17
<p>That's why programmers created several libraries that work similarly but provide many more features. Moreover, many of these libraries are isomorphic, so they work the same way both in the browser and on the server. One of the most popular libraries is<a>axios</a>.</p>
18
<h2>URL</h2>
18
<h2>URL</h2>
19
<p>As we know from previous courses, gluing lines to work with paths or URLs is not a great idea. You can easily make a mistake and end up doing work that a machine can do.</p>
19
<p>As we know from previous courses, gluing lines to work with paths or URLs is not a great idea. You can easily make a mistake and end up doing work that a machine can do.</p>
20
<p>You can always use many third-party libraries or browsers' built-in mechanisms for it, or polyfills for older browsers:</p>
20
<p>You can always use many third-party libraries or browsers' built-in mechanisms for it, or polyfills for older browsers:</p>
21
<p>What's nice is that fetch can work with the URL object directly:</p>
21
<p>What's nice is that fetch can work with the URL object directly:</p>
22
<p>And here's how you can work with query parameters:</p>
22
<p>And here's how you can work with query parameters:</p>
23
<h2>HTTP access control (CORS)</h2>
23
<h2>HTTP access control (CORS)</h2>
24
<p>Attackers can use client HTTP requests to steal data. Therefore, browsers control where and how requests are made. You can read more about this mechanism<a>here</a>.</p>
24
<p>Attackers can use client HTTP requests to steal data. Therefore, browsers control where and how requests are made. You can read more about this mechanism<a>here</a>.</p>