You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
**XMLHttpRequest** is an objects to interact with servers. You can retrieve data from a URL without having to do a full page refresh. XMLHttpRequest is used heavily in Ajax programming - [MDN](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest).
17
+
**XMLHttpRequest** is an object to interact with servers. You can retrieve data from a URL without having to do a full page refresh. XMLHttpRequest is used heavily in Ajax programming - [MDN](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest).
18
18
19
19
So what is Ajax?
20
20
**Ajax** is a method of exchanging data with a server, and updating parts of a web page without reloading the entire page.
21
21
22
-
Let's diving into the code:
22
+
Let's dive into the code:
23
23
24
24
First, we need to make an instance from 'XMLHttpRequest' object.
25
-
```javascript
25
+
```js
26
26
var http =newXMLHttpRequest();
27
27
```
28
28
When we are doing a request it goes through 5 states:
@@ -32,8 +32,8 @@ When we are doing a request it goes through 5 states:
32
32
* 3 : request is in process.
33
33
* 4 : request is complete.
34
34
35
-
In the code below we are checking if the request is complete or not, and we check the status == 200 just to make sure that we do not get 404 error - Take a look about [HTTP Status Code](https://httpstatuses.com).
36
-
```javascript
35
+
In the code below we are checking if the request is complete or not, and we check the status == 200 just to make sure that we do not get a 404 error - Read more about it here:[HTTP Status Code](https://httpstatuses.com).
36
+
```js
37
37
http.onreadystatechange=function() {
38
38
if ( http.readyState==4&&http.status==200) {
39
39
console.log('Response from the server: '+http.response);
@@ -48,16 +48,16 @@ There are methods to deal with a server like (GET, POST, UPDATE, DELETE…)
48
48
* DELETE: delete date from the server.
49
49
50
50
To initialize a request we use [open](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/open) method. The syntax is:
The parameters _method_ and _url_ are mandatory, _user_ and _password_ are optional. True is a default value for _async_.
55
55
56
-
```javascript
56
+
```js
57
57
http.open("GET", URL, true/false);
58
58
```
59
59
At the end we have to send our request to the server through [send](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send) method. In our situation we are retrieving a data from the server, so we do not have to pass a parameter to the send request.
0 commit comments