1
我有一個頁面,我在wordpress中顯示自定義類型的帖子。基本上,有一些複選框可以根據使用Ajax的分類來搜索帖子。但結果顯示0在隱身模式和Firefox。但是,在我的谷歌瀏覽器中,它工作正常。我已經使用die()和ajax回調被正確定義。你能否檢查我失蹤的地方?我有ajax.php
文件包含在functions.php
,爲什麼Ajax搜索返回0?
<?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的文件如下:
(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);
它在我的瀏覽器工作正常,但在Chrome的隱身模式和其他瀏覽器如Firefox,它返回0.它在我的朋友的谷歌瀏覽器中也返回0。你能猜出這個錯誤嗎?在我的瀏覽器中,搜索工作正常。
https://www.google.co.uk/search?q=ajax+returns+0&oq=ajax+returns+0&aqs=chrome..69i57j0l5.1693j0j7&sourceid=chrome&ie=UTF-8 - 以上任何相關內容? – ADyson
沒有.... Intrestingly,在我的Chrome瀏覽器,它正在工作。但在其他瀏覽器中,結果爲0.另外,它在我的朋友的谷歌瀏覽器中返回0。但它在我的Chrome瀏覽器中工作正常(不是隱身)。 – Amit
'wp_ajax_no_priv_search' - >'wp_ajax_nopriv_search' – user8262086