wip found how to theorically get ajax request in php but for the moment i get error 400
This commit is contained in:
2
plugins/wp_model_plugin/html/menu/menu.html
Normal file
2
plugins/wp_model_plugin/html/menu/menu.html
Normal file
@@ -0,0 +1,2 @@
|
||||
<input type='text' id='mytext'>
|
||||
<button id='mybutton'>send</button>
|
||||
18
plugins/wp_model_plugin/js/menu/menu.js
Normal file
18
plugins/wp_model_plugin/js/menu/menu.js
Normal file
@@ -0,0 +1,18 @@
|
||||
const inputElement = document.getElementById('mytext');
|
||||
const sendButton = document.getElementById('mybutton');
|
||||
|
||||
sendButton.addEventListener('click', () => {
|
||||
const inputValue = inputElement.value;
|
||||
const myurl = php_data.ajax_url;
|
||||
console.log(myurl);
|
||||
|
||||
fetch(myurl, {
|
||||
method: 'POST',
|
||||
credentials: 'same-origin',
|
||||
body: JSON.stringify({
|
||||
action: 'get_data',
|
||||
_ajax_nonce: php_data.nonce,
|
||||
data: inputValue,
|
||||
})
|
||||
})
|
||||
});
|
||||
@@ -1,27 +1,21 @@
|
||||
<?php
|
||||
|
||||
function wp_model_plugin_content() {
|
||||
echo "
|
||||
<input type='text' id='mytext'>
|
||||
<button id='mybutton'>send</button>
|
||||
add_files_to_front( array(
|
||||
"menu/menu.js",
|
||||
));
|
||||
|
||||
<script>
|
||||
const inputElement = document.getElementById('mytext');
|
||||
const sendButton = document.getElementById('mybutton');
|
||||
$ajax_url = admin_url( 'admin-ajax.php' );
|
||||
$nonce = wp_create_nonce( 'wp-pageviews-nonce' );
|
||||
add_var_to_front(
|
||||
compact(
|
||||
"ajax_url",
|
||||
"nonce",
|
||||
),
|
||||
"menu"
|
||||
);
|
||||
|
||||
sendButton.addEventListener('click', () => {
|
||||
const inputValue = inputElement.value;
|
||||
const myurl = php_data.ajax_url;
|
||||
console.log(myurl);
|
||||
|
||||
fetch(url, {
|
||||
method: 'POST',
|
||||
credentials: 'same-origin',
|
||||
body: JSON.stringify({ data: inputValue })
|
||||
})
|
||||
});
|
||||
</script>
|
||||
";
|
||||
echo create_html("menu/menu.html");
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
@@ -55,9 +55,6 @@ function add_var_to_front($vars, $handle = "init") {
|
||||
$handle = pathinfo($handle, PATHINFO_FILENAME);
|
||||
$object_name = "php_data";
|
||||
|
||||
$ajax_url = admin_url( 'admin-ajax.php' );
|
||||
$vars["ajax_url"] = $ajax_url;
|
||||
|
||||
wp_localize_script($handle, $object_name, $vars);
|
||||
}
|
||||
|
||||
|
||||
@@ -21,10 +21,11 @@ in opposition to the methode file_get_contents()
|
||||
|
||||
https://stackoverflow.com/a/4402045/9497573
|
||||
*/
|
||||
function create_html($files, $vars) {
|
||||
function create_html($files, $vars = null) {
|
||||
$files = (array)$files;
|
||||
$html_dir = PLUGIN_DIR.'html/';
|
||||
extract($vars);
|
||||
if (!is_null($vars))
|
||||
extract($vars);
|
||||
|
||||
ob_start();
|
||||
foreach($files as $file) {
|
||||
|
||||
@@ -93,11 +93,13 @@ function plugin_menu() {
|
||||
}
|
||||
add_action('admin_menu', 'plugin_menu');
|
||||
|
||||
add_action( 'wp_ajax_nopriv_get_data', 'my_ajax_handler' );
|
||||
add_action( 'wp_ajax_get_data', 'my_ajax_handler' );
|
||||
|
||||
function my_ajax_handler() {
|
||||
console_log("in my_ajax_handler");
|
||||
console_log("data: ");
|
||||
console_log($data);
|
||||
wp_send_json_success( 'It works' );
|
||||
}
|
||||
add_action( 'wp_ajax_get_data', 'my_ajax_handler' );
|
||||
|
||||
|
||||
?>
|
||||
|
||||
Reference in New Issue
Block a user