wip try better code organisation with ajax
This commit is contained in:
29
plugins/wp_model_plugin/js/ajax.js
Normal file
29
plugins/wp_model_plugin/js/ajax.js
Normal 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);
|
||||
}
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user