2

wordpressでajaxを呼び出そうとしています。ブラウザでは正常に動作しますが、モバイル デバイスでは応答 0 を返します。これが私のワードプレスと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(
4

1 に答える 1

4

ログインしていないユーザーに問題がある可能性があります。ログインしていないユーザーには以下の構文を使用してください

add_action('wp_ajax_nopriv_getnearest', 'get_nearest_destinations'); // Not logged in user
于 2013-11-01T07:20:13.563 に答える