wip try better code organisation with ajax

This commit is contained in:
asus
2024-02-10 12:27:09 +01:00
parent 0b2e6352e1
commit f6ba2a9e2c

View File

@@ -0,0 +1,29 @@
function ajax_post(data, action, callback_response, callback_error) {
const data = new FormData();
data.append('action', action);
data.append('_ajax_nonce', php_data.nonce);
data.append('data', data);
fetch(php_data.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);
}
});
};