我试图在wordpress中调用ajax。它在浏览器中工作正常但在移动设备中它返回响应0。这是我的wordpress& Jquery代码。请建议我在哪里做错了
functions.php文件中的代码
function get_nearest_destinations()
{
$data = array();
check_ajax_referer( "getnearestdestinations" );
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => -1,
);
$the_query = new WP_Query($args);
if($the_query->have_posts()){
while ( $the_query->have_posts() ) :
$the_query->the_post();
$data[] = array('title'=>get_the_title());
endwhile;
}
echo json_encode($data); exit();
}
add_action( 'wp_ajax_getnearest', 'get_nearest_destinations' );
下面是模板文件中的js代码....
<?php $nonce = wp_create_nonce( 'getnearestdestinations' ); ?>
jQuery.ajax({
type: "POST",
url: "<?php echo bloginfo('url').'/wp-admin/admin-ajax.php'; ?>",
data: { action: 'getnearest', _ajax_nonce: '<?php echo $nonce; ?>'},
dataType: "json",
success: function(html){
alert(html)
}
}); //close jQuery.ajax(
答案 0 :(得分:4)
可能是您有未登录的用户问题.Pl使用以下语法为非登录用户
add_action('wp_ajax_nopriv_getnearest', 'get_nearest_destinations'); // Not logged in user