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});
1fetch('https://example.com/profile', {
2 method: 'POST',
3 headers: { 'Content-Type': 'application/json' },
4 body: JSON.stringify({
5 'foo': 'bar'
6 }),
7})
8 .then((res) => res.json())
9 .then((data) => {
10 // Do some stuff ...
11 })
12 .catch((err) => console.log(err));
1 $.ajax({
2 url : 'more_com.php', //PHP file to execute
3 type : 'GET', //method used POST or GET
4 data : {variable1 : "some data"}, // Parameters passed to the PHP file
5 success : function(result){ // Has to be there !
6
7 },
8
9 error : function(result, statut, error){ // Handle errors
10
11 }
12
13 });
14
15// NOTE : Parameters will be available either through $_GET or $_POST according
16// to the method you choosed to use.
17// Here you will get your variable "variable1" this way : $_GET['variable1']
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
1$.ajax({
2 url: 'ajaxfile.php',
3 type: 'post',
4 data: {name:'yogesh',salary: 35000,email: 'yogesh@makitweb.com'},
5 success: function(response){
6
7 }
8});
1<!DOCTYPE html>
2<html>
3<body>
4
5<h1>The XMLHttpRequest Object</h1>
6
7<p id="demo">Let AJAX change this text.</p>
8
9<button type="button" onclick="loadDoc()">Change Content</button>
10
11<script>
12function loadDoc() {
13 var xhttp = new XMLHttpRequest();
14 xhttp.open("GET", "ajax_info.txt", false);
15 xhttp.send();
16 document.getElementById("demo").innerHTML = xhttp.responseText;
17}
18</script>
19
20</body>
21</html>
22