1let data = {element: "barium"};
2
3fetch("/post/data/here", {
4 method: "POST",
5 body: JSON.stringify(data)
6}).then(res => {
7 console.log("Request complete! response:", res);
8});
9
10
11// If you are as lazy as me (or just prefer a shortcut/helper):
12
13window.post = function(url, data) {
14 return fetch(url, {method: "POST", body: JSON.stringify(data)});
15}
16
17// ...
18
19post("post/data/here", {element: "osmium"});
20
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 src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>