how to put ajax call in function code example
Example 1: make ajax calls with jQuery
$.ajax({
url: "example.php?firstParam=Hello&secondParam=World",
dataType: 'json',
timeout: 2000
}).done(function (data, textStatus, jqXHR) {
}).fail(function (jqXHR, textStatus, errorThrown) {
console.log("jqXHR:" + jqXHR);
console.log("TestStatus: " + textStatus);
console.log("ErrorThrown: " + errorThrown);
});
var formData = {name: "John", surname: "Doe", age: "31"};
$.ajax({
url: "example.php",
type: "POST",
data: formData,
timeout: 2000,
async: false,
success: function (data, textStatus, jqXHR) {
},
error: function (jqXHR, textStatus, errorThrown) {
console.log("jqXHR:" + jqXHR);
console.log("TestStatus: " + textStatus);
console.log("ErrorThrown: " + errorThrown);
}
});
$.ajax({
url: "api.php?action=getCategories",
dataType: 'json',
timeout: 2000,
success: function (result, textStatus, jqXHR) {
},
error: function (jqXHR, textStatus, errorThrown) {
console.log("jqXHR:" + jqXHR);
console.log("TestStatus: " + textStatus);
console.log("ErrorThrown: " + errorThrown);
}
});
Example 2: how to add ajax within ajax
$.ajax({
type: "post",
url: "ajax/example.php",
data: 'page=' + btn_page,
success: function (data) {
var a = data;
$.ajax({
type: "post",
url: "example.php",
data: 'page=' + a,
success: function (data) {
}
error: function (xhr) {
console.log(xhr.responseText);
}
});
}
});