duanjie9630 2017-09-16 18:46
浏览 77
已采纳

为什么Ajax搜索返回0?

I have a page where I am showing the posts of a custom type in wordpress. Basically, there are checkboxes to search posts based on taxonomy using Ajax. But the result is showing 0 in incognito mode and Firefox. But, in my google chrome, it is working fine. I have used die() and ajax callback is properly defined. Could you please check that where I am missing. I have ajax.php file which is included in functions.php,

<?php 
add_action('wp_head', 'wp_ajax_url');
add_action('wp_enqueue_scripts', 'include_ajax_js');

function wp_ajax_url(){
    $script = '<script>';
    $script .= 'var ajax_url = " ' . admin_url('admin-ajax.php') . ' " ; ' ;
    $script .= '</script>'; 
    echo $script;
}

function include_ajax_js() {
    wp_enqueue_script('ajax_js', get_template_directory_uri() . '/inc/jquery.js', array('jquery'), null, true);
}

add_action('wp_ajax_search', 'search');
add_action('wp_ajax_no_priv_search', 'search');

function search(){
    $skill = $_POST['skill'];
    $type = $_POST['type'];
    $certification = $_POST['certification'];
    //echo $skill, $type, $certification; die();

    if(!empty($skill) && !empty($type) && !empty($certification)){
    $args = array(
        'post_type' => 'myguncourse',
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'skilllevel',
                'field'    => 'slug',
                'terms'    => array( $skill ),
            ),
            array(
                'taxonomy' => 'firearmtype',
                'field'    => 'slug',
                'terms'    => array( $type ),
            ),
            array(
                'taxonomy' => 'certification',
                'field'    => 'slug',
                'terms'    => array( $certification ),
            ),
        ),
    );

} else if ( !empty($skill) && !empty($type )) {
    $args = array(
        'post_type' => 'myguncourse',
        'tax_query' => array(
            array(
                'taxonomy' => 'skilllevel',
                'field'    => 'slug',
                'terms'    => array( $skill ),
            ),
            array(
                'taxonomy' => 'firearmtype',
                'field'    => 'slug',
                'terms'    => array( $type ),
            ),
        ),
    );
} else if ( !empty($skill) && !empty($certification)) {
    $args = array(
        'post_type' => 'myguncourse',
        'tax_query' => array(
            array(
                'taxonomy' => 'skilllevel',
                'field'    => 'slug',
                'terms'    => array( $skill ),
            ),
            array(
                'taxonomy' => 'certification',
                'field'    => 'slug',
                'terms'    => array( $certification ),
            ),
        ),
    );
} else if ( !empty($type) && !empty($certification)) {
    $args = array(
        'post_type' => 'myguncourse',
        'tax_query' => array(
            array(
                'taxonomy' => 'firearmtype',
                'field'    => 'slug',
                'terms'    => array( $type ),
            ),
            array(
                'taxonomy' => 'certification',
                'field'    => 'slug',
                'terms'    => array( $certification ),
            ),
        ),
    );
} else if ( !empty($skill)) {
    $args = array(
        'post_type' => 'myguncourse',
        'tax_query' => array(
            array(
                'taxonomy' => 'skilllevel',
                'field'    => 'slug',
                'terms'    => array( $skill ),
            ),

        ),
    );
} else if ( !empty($type)) {
    $args = array(
        'post_type' => 'myguncourse',
        'tax_query' => array(
            array(
                'taxonomy' => 'firearmtype',
                'field'    => 'slug',
                'terms'    => array( $type ),
            ),

        ),
    );
}  else if ( !empty($certification)) {
    $args = array(
        'post_type' => 'myguncourse',
        'tax_query' => array(
            array(
                'taxonomy' => 'certification',
                'field'    => 'slug',
                'terms'    => array( $certification ),
            ),

        ),
    );
}  else {
    $args = array(
        'post_type' => 'myguncourse',
    );
}

    $query = new WP_Query($args); 

    if( $query->have_posts()) :
    while ( $query->have_posts() ) : $query->the_post();
    ?>
   <h4 style="display: inline;"><?php the_title();?></h4>
   <h5 style="display:inline;" class="pull-right"></h5>
   <p><?php the_content();?></p> 
   <a href="<?php the_permalink();?>" align="right"><b>VIEW THIS COURSE </b></a><hr>

   <?php
    endwhile; endif;
    //wp_reset_query();
    die();     
}
?>

Andmy jquery file is as follows:

(function($) {


 $(".chb1").change(function () {
 $(".chb1").not(this).prop('checked', false);
 });

 $(".chb2").change(function () {
 $(".chb2").not(this).prop('checked', false);
 });

 $(".chb3").change(function () {
 $(".chb3").not(this).prop('checked', false);
 });


 $('#myForm input').on('change', function() {
  skill = $('input[name=skill]:checked', '#myForm').val();
  type = $('input[name=type]:checked', '#myForm').val();
  certification = $('input[name=certification]:checked', '#myForm').val(); 
   $.ajax({
      type: 'post',
      url: ajax_url,
      data: { action: 'search', skill : skill, type : type, certification : certification },
      success: function(response){
          $('.article').html(response);
          //console.log(response);
      }
  })
 });

})(jQuery);

It is working fine in my browser, but in chrome's incognito mode and other browser like firefox, it's returning 0. It is also returning 0 in my friend's google chrome. Could you please guess the mistake. In my browser, search is working fine.

  • 写回答

1条回答 默认 最新

  • duanbo6871 2017-09-17 06:10
    关注

    Yes! You are doing a mistake here:

    add_action('wp_ajax_no_priv_search', 'search');
    

    So, the users who are not logged in, will not able to see the result. You must correct that to:

    add_action('wp_ajax_nopriv_search', 'search');
    

    nopriv is used for the users who are not logged in. so you were not able to see the result.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 软件定义网络mininet和onos控制器问题
  • ¥15 微信小程序 用oss下载 aliyun-oss-sdk-6.18.0.min client报错
  • ¥15 ArcGIS批量裁剪
  • ¥15 labview程序设计
  • ¥15 为什么在配置Linux系统的时候执行脚本总是出现E: Failed to fetch http:L/cn.archive.ubuntu.com
  • ¥15 Cloudreve保存用户组存储空间大小时报错
  • ¥15 伪标签为什么不能作为弱监督语义分割的结果?
  • ¥15 编一个判断一个区间范围内的数字的个位数的立方和是否等于其本身的程序在输入第1组数据后卡住了(语言-c语言)
  • ¥15 Mac版Fiddler Everywhere4.0.1提示强制更新
  • ¥15 android 集成sentry上报时报错。