为了账号安全,请及时绑定邮箱和手机立即绑定

Wordpress ajax 请求无法正常工作

Wordpress ajax 请求无法正常工作

PHP
千巷猫影 2022-06-17 09:58:26
我是 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);


查看完整回答
反对 回复 2022-06-17
  • 1 回答
  • 0 关注
  • 84 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号