javascript ajax call promise code example
Example 1: js promis with ajax
function doTheThing() {
return new Promise((resolve, reject) => {
$.ajax({
url: window.location.href,
type: 'POST',
data: {
key: 'value',
},
success: function (data) {
resolve(data)
},
error: function (error) {
reject(error)
},
})
})
}
Example 2: javascript promise with ajax
doTheThing()
doSomethingElse()
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);
});