1const Http = new XMLHttpRequest();
2const url='https://jsonplaceholder.typicode.com/posts';
3Http.open("GET", url);
4Http.send();
5
6Http.onreadystatechange = (e) => {
7 console.log(Http.responseText)
8}
1// Use these functions:
2
3function _GET_REQUEST(url, response) {
4 var xhttp;
5 if (window.XMLHttpRequest) {
6 xhttp = new XMLHttpRequest();
7 } else {
8 xhttp = new ActiveXObject("Microsoft.XMLHTTP");
9 }
10
11 xhttp.onreadystatechange = function() {
12 if (this.readyState == 4 && this.status == 200) {
13 response(this.responseText);
14 }
15 };
16
17 xhttp.open("GET", url, true);
18 xhttp.send();
19}
20
21function _POST_REQUEST(url, params, response) {
22 var xhttp;
23 if (window.XMLHttpRequest) {
24 xhttp = new XMLHttpRequest();
25 } else {
26 xhttp = new ActiveXObject("Microsoft.XMLHTTP");
27 }
28
29 xhttp.onreadystatechange = function() {
30 if (this.readyState == 4 && this.status == 200) {
31 response(this.responseText);
32 }
33 };
34
35 xhttp.open("POST", url, true);
36 xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
37 xhttp.send(params);
38}
39
40
41// Use like:
42_GET_REQUEST('http://url.com', (response) => {
43 // Do something with variable response
44 console.log(response);
45});
46_POST_REQUEST('http://url.com', 'parameter=sometext', (response) => {
47 // Do something with variable response
48 console.log(response);
49});
1function httpGetAsync(url, callback) {
2 var xmlHttp = new XMLHttpRequest();
3 xmlHttp.onreadystatechange = function() {
4 if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
5 callback(xmlHttp.responseText);
6 }
7 xmlHttp.open("GET", url, true); // true for asynchronous
8 xmlHttp.send(null);
9}
1<script>
2function loadDoc() {
3 var xhttp = new XMLHttpRequest();
4
5 //looking for a change or state , like a request or get.
6 xhttp.onreadystatechange = function() {
7
8 //if equal to 4 means that its ready.
9 // if equal to 200 indicates that the request has succeeded.
10 if (this.readyState == 4 && this.status == 200) {
11 document.getElementById("demo").innerHTML = this.responseText;
12 }
13 };
14
15 //GET method for gettin the data // the file you are requesting
16 xhttp.open("GET", "TheFileYouWant.html", true);
17
18 //sending the request
19 xhttp.send();
20}
1<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>