Skip to content

Instantly share code, notes, and snippets.

@Tusko
Last active November 12, 2021 02:54
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Tusko/dfe1fcbbb71fc0a36c86cdd79530d1fc to your computer and use it in GitHub Desktop.
Save Tusko/dfe1fcbbb71fc0a36c86cdd79530d1fc to your computer and use it in GitHub Desktop.
$.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
Copy link

osw4l commented Aug 10, 2017

thanks, its works !

@Sarfroz
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