我正在尝试在wordpress中调用ajax。它在浏览器中工作正常,但在移动设备中返回响应 0 .这是我的wordpress和Jquery代码。请指出我做错的地方
函数.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(
可能是
您有未登录的用户问题。请为非登录用户使用以下语法
add_action('wp_ajax_nopriv_getnearest', 'get_nearest_destinations'); // Not logged in user