ajax script is automatically added when a script is added

This commit is contained in:
asus
2024-02-11 18:17:05 +01:00
parent 87a0f7fc0b
commit 322e440422
6 changed files with 94 additions and 48 deletions

View File

@@ -0,0 +1,36 @@
/*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/
function ajax_post(ajax_data, action, callback_response, callback_error) {
console.log("in ajac_post, ajax_data :");
console.log(ajax_data);
const data = new FormData();
data.append('action', action);
data.append('_ajax_nonce', nonce);
data.append('data', ajax_data);
console.log("data: ");
console.log(data);
fetch(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);
}
});
};