1var formData = {name:"John", surname:"Doe", age:"31"}; //Array
2
3$.ajax({
4 url : "https://example.com/rest/getData", // Url of backend (can be python, php, etc..)
5 type: "POST", // data type (can be get, post, put, delete)
6 data : formData, // data in json format
7 async : false, // enable or disable async (optional, but suggested as false if you need to populate data afterwards)
8 success: function(response, textStatus, jqXHR) {
9 console.log(response);
10 },
11 error: function (jqXHR, textStatus, errorThrown) {
12 console.log(jqXHR);
13 console.log(textStatus);
14 console.log(errorThrown);
15 }
16});
1<button id="ajaxButton" type="button">Make a request</button>
2
3<script>
4(function() {
5 var httpRequest;
6 document.getElementById("ajaxButton").addEventListener('click', makeRequest);
7
8 function makeRequest() {
9 httpRequest = new XMLHttpRequest();
10
11 if (!httpRequest) {
12 alert('Giving up :( Cannot create an XMLHTTP instance');
13 return false;
14 }
15 httpRequest.onreadystatechange = alertContents;
16 httpRequest.open('GET', 'test.html');
17 httpRequest.send();
18 }
19
20 function alertContents() {
21 if (httpRequest.readyState === XMLHttpRequest.DONE) {
22 if (httpRequest.status === 200) {
23 alert(httpRequest.responseText);
24 } else {
25 alert('There was a problem with the request.');
26 }
27 }
28 }
29})();
30</script>
31