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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 非root手机,如何精准控制手机流量消耗的大小,如20M
  • ¥15 远程安装一下vasp
  • ¥15 自己做的代码上传图片时,报错
  • ¥15 Lingo线性规划模型怎么搭建
  • ¥15 关于#python#的问题,请各位专家解答!区间型正向化
  • ¥15 unity从3D升级到urp管线,打包ab包后,材质全部变紫色
  • ¥50 comsol温度场仿真无法模拟微米级激光光斑
  • ¥15 上传图片时提交的存储类型
  • ¥15 VB.NET如何绘制倾斜的椭圆
  • ¥15 arbotix没有/cmd_vel话题