From f6ba2a9e2c968fa3bbe1f915ecd2b5ebe3beba6c Mon Sep 17 00:00:00 2001 From: asus Date: Sat, 10 Feb 2024 12:27:09 +0100 Subject: [PATCH] wip try better code organisation with ajax --- plugins/wp_model_plugin/js/ajax.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 plugins/wp_model_plugin/js/ajax.js diff --git a/plugins/wp_model_plugin/js/ajax.js b/plugins/wp_model_plugin/js/ajax.js new file mode 100644 index 0000000..e53fe4c --- /dev/null +++ b/plugins/wp_model_plugin/js/ajax.js @@ -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); + } + }); +};