dtrhd2850 2017-06-21 20:58 采纳率: 0%
浏览 56

AJAX在Wordpress中返回0

I am trying to get my ajax request to work but only get returned 0; not sure exactly why i'm not getting a response back. I dont really fully understand how the my_ajax_pagination() function handles. I was just finding other solutions online and found something like that.

$(document).on( 'click', 'polygon', function( event ) {

 var county = $(this).attr('id')
 var region = $(this).closest('a').data('region');
    jQuery.ajax(
        {
            type: 'POST',
            url: ajaxpagination.ajaxurl,
                dataType:'json',
                        data: {
                                    action: 'my_ajax_pagination',

                                },

            success: function(data){
                console.log(data);
            },
                        error:function(err){
                            console.log('err')
                        }
        });
    })
})(jQuery);

functions.php

            function my_enqueue_assets() {
                wp_enqueue_style( 'parent-style', get_template_directory_uri().'/style.css' );
                wp_enqueue_script( 'ajax-pagination',  get_stylesheet_directory_uri() . '/js/ajax-pagination.js', array( 'jquery' ), '1.0', true );

                    global $wp_query;

                    wp_localize_script( 'ajax-pagination', 'ajaxpagination', array(
                        'ajaxurl' => admin_url( 'admin-ajax.php' ),
                        'query_vars' => json_encode( $wp_query->query ),
                      'serurity' => wp_create_nonce('wp-admin-order')
                    ));
            };

            function my_ajax_pagination() {
              $query_vars = json_decode( stripslashes( $_POST['query_vars'] ), true );
              $query_vars['county'] = $_POST['county'];
              $query_vars['region'] = $_POST['region'];

                $query = new WP_Query( array(
                        'post_type' => 'resource',
                        'posts_per_page'=> -1
                ) );
                echo $query_vars;
                die();
            };
        add_action( 'wp_ajax_nopriv_ajax_pagination', 'my_ajax_pagination' );
        add_action( 'wp_ajax__ajax_pagination', 'my_ajax_pagination' );
  • 写回答

1条回答 默认 最新

  • douzhaolu4780 2017-06-21 21:09
    关注

    The reason you are getting 0 is that no function is called in WordPress. The action that you send to ajax should be ajax_pagination and not my_ajax_pagination, the action should be the same name as wp_ajax_{name}.

    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 虚心请教几个问题,小生先有礼了
  • ¥30 截图中的mathematics程序转换成matlab