Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
$.ajax Promise
/*
* ajax Defaults (optional):
$.ajaxSetup({
type : 'POST',
dataType : 'json',
cache : true,
global : true,
data : {},
contentType : 'application/json',
beforeSend : function (xhr) {
xhr.setRequestHeader('X-Token', 'MY-API-TOKEN');
}
});
*/
function ajax(options) {
return new Promise(function (resolve, reject) {
$.ajax(options).done(resolve).fail(reject);
});
}
then run:
ajax({
url: YOUR_URL,
type: 'post',
dataType: 'json',
data: {
action: some_action,
action_2: another_action
}
}).then(
function fulfillHandler(data) {
// callback
}
).catch(function errorHandler(error) {
// error
});
@osw4l

This comment has been minimized.

Copy link

osw4l commented Aug 10, 2017

thanks, its works !

@Sarfroz

This comment has been minimized.

Copy link

Sarfroz commented Mar 6, 2018

i have the query that I am using for loop and from there I am calling the function that will run the ajax. but problem is that my whole loop gets executed not one by one and due to this , at php side the check code not work. But if i make async:false code works fine. Any solution for this problem ??

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.