我是 wordpress 的新手,我在单击按钮上发出 ajax 请求并打印数据,但 ajax 没有给我任何响应。请帮我找出错误。这是我的代码add_action("wp_ajax_delivery_options", "delivery_options");add_action("wp_ajax_nopriv_delivery_options", "delivery_options");function delivery_options(){ echo json_encode(array('type' => 'success')); wp_die();}wp_enqueue_script("my-ajax-handle", get_stylesheet_directory_uri() . "/js/custom.js", array('jquery'));wp_localize_script('my-ajax-handle', 'the_ajax_script', array('ajaxurl' => admin_url('admin-ajax.php')));阿贾克斯(function($) { $(document).ready(function() { $('#delivery_option button').on('click', function(e) { e.preventDefault(); var data = e.currentTarget.id; $.ajax({ type: 'POST', dataType: 'json', url: the_ajax_script.ajaxurl, data: { delivery_option: data }, success: function(response) { console.log(response); } }); }); });})(jQuery);任何解决方案表示赞赏!
1 回答

慕婉清6462132
TA贡献1804条经验 获得超2个赞
你必须通过“回调函数名”data: { action: 'delivery_options', delivery_option: data },
(function($) {
$(document).ready(function() {
$('#delivery_option button').on('click', function(e) {
e.preventDefault();
var data = e.currentTarget.id;
$.ajax({
type: 'POST',
dataType: 'json',
url: the_ajax_script.ajaxurl,
data: { action: 'delivery_options', delivery_option: data },
success: function(response) {
console.log(response);
}
});
});
});})(jQuery);
- 1 回答
- 0 关注
- 84 浏览
添加回答
举报
0/150
提交
取消