douzou8074 2015-03-20 19:52
浏览 105
已采纳

WordPress Ajax请求返回0

I can't figure out why nothing is returned I am a real beginner in Ajax .. I just read a lot of topics about using Ajax in Woprdpress but the examples are super advanced for me Here is my JS code combo_checkout_iRange.js

jQuery(document).ready(function() {
    jQuery('#loader').hide();
    jQuery('#check-out-date').hide();
    jQuery('#check-in-date').change(function(){
        jQuery('#check-out-date').fadeOut();
        jQuery('#loader').show();
        jQuery.ajax({
            type: 'POST',
            url:myAjax.ajaxurl, 
            data: {
                action : 'my_ajax_handler',
                parent_id: jQuery("#check-in-date").val()
            },
            success: function(data){
                alert(data);
                jQuery('#loader').hide();
                jQuery('#check-out-date').show();
                jQuery('#check-out-date').append(data);
            }});
            return false;
        });
        jQuery('#check-out-date').change(function(){
            alert(jQuery('#check-out-date').val());
        });
    });

This is what I wrote on function.php

Note: this should work in post type called "meetings"

add_action("wp_enqueue_scripts", function() {

     if (is_single()) {
        if (get_post_type() == 'meetings')
        {
            wp_enqueue_script('combo_checkout_iRange', get_template_directory_uri() . '/js/combo_checkout_iRange.js', array( 'jquery' ), '1.0' ,true);

            $data_array = array(
                'ajaxurl' => admin_url( 'admin-ajax.php' )
            );

            wp_register_script( 'combo_checkout_iRange', get_template_directory_uri() . '/js/combo_checkout_iRange.js', array('jquery') );
            wp_localize_script( 'combo_checkout_iRange', 'myAjax', $data_array );



        }
    }
});

and this is my ajax handler i put it inside single_meetings.php

add_action("wp_ajax_my_ajax_handler", "my_ajax_handler");
add_action("wp_ajax_nopriv_my_ajax_handler", "my_ajax_handler");

function my_ajax_handler() {
                if ( isset($_REQUEST["parent_id"]) ) {
                     $id    = $_REQUEST["parent_id"];
                     return $id;
                     die();
                 }
             }
  • 写回答

1条回答 默认 最新

  • duanpai9945 2015-03-20 20:17
    关注

    You can't use return in your AJAX callback. The code never gets to die on the line beneath. You need to echo the value instead and then use wp_die().

    Replace:

    return $id;
    die();
    

    With:

    echo $id;
    wp_die();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵