jquery ajax then 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: ajax jquery example
$.ajax({
url : "file.php",
method : "POST",
data: {
action : action ,
key_1 : value_key_1,
key_2 : value_key_2
}
})
.fail(function() { return false; })
.done(function(data) {
console.log(data);
});
Example 3: jquery ajax promise
$.ajax({
url: "/someurl",
method: "GET",
data: {
a: "a"
})
.done(function(data) {
console.log('success callback 1', data)
})
.done(function(data) {
console.log('success callback 2', data)
})
.fail(function(xhr) {
console.log('error callback 1', xhr);
})
.fail(function(xhr) {
console.log('error callback 2', xhr);
});