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 envi深度学习模块错误原因
  • ¥15 孟德尔随机化混杂因素
  • ¥15 关于react-hook组件用函数控制是否渲染的及时性问题。
  • ¥50 Linux下的软件,要做模块化拆分。进程间通信是否有开源框架可以借用?
  • ¥100 修改原有的MYSQL存储代码,在最右边添加多列数据
  • ¥20 Open Interpreter 使用时报错: still has pending operation at deallocation, the process may crash
  • ¥15 qt中链接动态链接库,调用其中的函数,该函数的参数需要传入回调函数,自己创建的回调函数无法作为参数传递进去
  • ¥15 matlab svm二分类代码问题
  • ¥40 求一款能支持ios15以上的屏蔽越狱插件。比较好用的
  • ¥15 C++ QT对比内存字符(形式不定)