我是 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 關注
- 78 瀏覽
添加回答
舉報
0/150
提交
取消