Files
2024_WEBSITE_fipf/plugins/fipfcard_plugin/js/utils/ajax.js
2024-02-12 10:13:02 +01:00

35 lines
806 B
JavaScript

/*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
function that create an ajax post action
it can be "overloaded" with a callback_response and _error
*/
function ajax_post(ajax_data, action, callback_response, callback_error) {
const data = new FormData();
data.append('action', action);
data.append('_ajax_nonce', wp_ajax._nonce);
data.append('data', ajax_data);
fetch(wp_ajax._url, {
method: 'POST',
credentials: 'same-origin',
body: data
})
.then((response) => response.json())
.then((data) => {
if (callback_response)
callback_response(data);
else {
console.log("data: ");
console.log(data);
}
})
.catch((error) => {
if (callback_error)
callback_error(error);
else {
console.log("error: ");
console.log(error);
}
});
};