Skip to content

Commit cd0ccc5

Browse files
committed
Ajax
1 parent 853df66 commit cd0ccc5

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

Diff for: README.md

+43
Original file line numberDiff line numberDiff line change
@@ -7683,6 +7683,49 @@ myContentType = myRequest.headers.get('Content-Type'); // returns 'image/jpeg'
76837683
<b><a href="#">↥ back to top</a></b>
76847684
</div>
76857685
7686+
## Q 15.12. Explain ajax request in javascript?
7687+
7688+
Ajax stands for Asynchronous Javascript And Xml. It load data from the server and selectively updating parts of a web page without reloading the whole page.
7689+
7690+
Basically, Ajax uses browser\'s built-in `XMLHttpRequest()` object to send and receive information to and from a web server asynchronously, in the background, without blocking the page or interfering with the user\'s experience.
7691+
7692+
<p align="center">
7693+
<img src="assets/ajax.jpg" alt="Ajax" width="500px" />
7694+
</p>
7695+
7696+
**Example:**
7697+
7698+
```js
7699+
(function() {
7700+
var xhr;
7701+
document.getElementById('ajaxButton').addEventListener('click', makeRequest);
7702+
7703+
function makeRequest() {
7704+
if (window.XMLHttpRequest) {
7705+
// code for IE7+, Firefox, Chrome, Opera, Safari
7706+
xhr = new XMLHttpRequest();
7707+
} else {
7708+
// code for IE6, IE5
7709+
xhr = new ActiveXObject('Microsoft.XMLHTTP');
7710+
}
7711+
xhr.onreadystatechange = function() {
7712+
if (this.readyState == 4 && this.status == 200) {
7713+
document.getElementById("result").innerHTML = '<pre>' + this.responseText + '</pre>';
7714+
}
7715+
};
7716+
xhr.open('GET', 'https://jsonplaceholder.typicode.com/posts', true); //this makes asynchronous true or false
7717+
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
7718+
xhr.send();
7719+
}
7720+
})();
7721+
```
7722+
7723+
**&#9885; [Try this example on CodeSandbox](https://codesandbox.io/s/js-ajax-ntgmov?file=/src/index.js)**
7724+
7725+
<div align="right">
7726+
<b><a href="#">↥ back to top</a></b>
7727+
</div>
7728+
76867729
## # 16. Collections
76877730
76887731
<br/>

Diff for: assets/ajax.jpg

57.3 KB
Loading

0 commit comments

Comments
 (0)