1$.ajax({
2 url:'your url',
3 type: 'POST', // http method
4 data: { myData: 'This is my data.' }, // data to submit
5 success: function (data, status, xhr) { // after success your get data
6 $('p').append('status: ' + status + ', data: ' + data);
7 },
8 error: function (jqXhr, textStatus, errorMessage) { // if any error come then
9 $('p').append('Error' + errorMessage);
10 }
11});
1// GET Request
2 $.ajax({
3 url: "example.php?firstParam=Hello&secondParam=World", //you can also pass get parameters
4 dataType: 'json', //dataType you expect in the response from the server
5 timeout: 2000
6 }).done(function (data, textStatus, jqXHR) {
7 //your code here
8 }).fail(function (jqXHR, textStatus, errorThrown) {
9 console.log("jqXHR:" + jqXHR);
10 console.log("TestStatus: " + textStatus);
11 console.log("ErrorThrown: " + errorThrown);
12 });
13
14//POST Request
15 var formData = {name: "John", surname: "Doe", age: "31"}; //Array
16 $.ajax({
17 url: "example.php",
18 type: "POST", // data type (can be get, post, put, delete)
19 data: formData, // data in json format
20 timeout: 2000, //Is useful ONLY if async=true. If async=false it is useless
21 async: false, // enable or disable async (optional, but suggested as false if you need to populate data afterwards)
22 success: function (data, textStatus, jqXHR) {
23 //your code here
24 },
25 error: function (jqXHR, textStatus, errorThrown) {
26 console.log("jqXHR:" + jqXHR);
27 console.log("TestStatus: " + textStatus);
28 console.log("ErrorThrown: " + errorThrown);
29 }
30 });
31
32
33//Alternatively, the old aproach is
34 $.ajax({
35 url: "api.php?action=getCategories",
36 dataType: 'json',
37 timeout: 2000,
38 success: function (result, textStatus, jqXHR) { //jqXHR = jQuery XMLHttpRequest
39 /*You could put your code here but this way of doing it is obsolete. Better to use .done()*/
40 },
41 error: function (jqXHR, textStatus, errorThrown) {
42 console.log("jqXHR:" + jqXHR);
43 console.log("TestStatus: " + textStatus);
44 console.log("ErrorThrown: " + errorThrown);
45 }
46 });
1/*SM*/
2$.ajax({
3 method: "POST",
4 url: "/controller/action",
5 data: { name: "John", location: "Boston" },
6 success: (result) => {
7 console.log(result);
8 },
9 error: (error) => {
10 console.log(error);
11 }
12 });
1var id = empid;
2
3$.ajax({
4 type: "POST",
5 url: "../Webservices/EmployeeService.asmx/GetEmployeeOrders",
6 data: "{empid: " + empid + "}",
7 contentType: "application/json; charset=utf-8",
8 dataType: "json",
9 success: function(result){
10 alert(result.d);
11 console.log(result);
12 }
13});
14
1 $.ajax({
2
3 url : "file.php",
4 method : "POST",
5
6 data: {
7 //key : value
8 action : action ,
9 key_1 : value_key_1,
10 key_2 : value_key_2
11 }
12 })
13
14 .fail(function() { return false; })
15 // Appel OK
16 .done(function(data) {
17
18 console.log(data);
19
20 });