我正在学习wordpress ajax调用,无法理解为什么我的特定调用返回了错误的请求。
这是回调函数:
wp_enqueue_script('demo-admin-js', plugins_url().'/demo/admin/js/demo-wp-ajax.js', array('jquery'));
wp_localize_script('demo-admin-js', 'demoWPAjax', array('security' => wp_create_nonce('abc')));
?>
<h1>First Setting</h1>
<form action="options.php" method="post">
<input type="checkbox" class="hide-admin-cb" id="hide-admin" name="first-option" value="yes"
<?php checked(get_option('first-option'), 'yes') ?>>
<label for="hide-admin">Hide Admin Bar on Frontend</label>
</form>
<?php
这是 Ajax.js
jQuery(function ($) {
$(document.getElementsByClassName('hide-admin-cb')).on('click', function(e) {
let data = {
action: 'demo-ajax-action-name',
security: demoWPAjax.security
};
$.ajax({
type: 'POST',
url: ajaxurl,
data: data,
success: function (response) {
alert('Success');
},
error: function (response) {
alert('Error!');
},
dataType: 'json'
})
});
});