douyong1974 2015-09-06 06:24
浏览 108
已采纳

Ajax在WordPress中不起作用

I am using ajax in WordPress and it is not working so can someone please help me out of this unwanted situation.

Here is my php code..

<div class="Publish">
        <h2>Published Pages</h2>
        <?php 
            $pages = new WP_Query(array('post_type'=>'page','post_status'=>'publish','posts_per_page'=>'-1','order'=>'ASC'));

       ?>
            <ul id="dashboard-page">
          <?php  if($pages -> have_posts()){
                while($pages -> have_posts()){
                    $pages->the_post(); ?>
                    <li id="<?php the_ID();?>" class="dashboard-item"><?php echo the_title(); ?></li>
                <?php }
            } ?>
            </ul>
        <input class="button-primary" id="submit" type="submit" name="submit" value="Click Me" />
        </div>

And here is the hook part.

    function ajax_action_stuff() {

    // doing ajax stuff
    update_option('vipin');
    die(); // stop executing script
}
add_action( 'wp_ajax_ajax_action', 'ajax_action_stuff' ); // ajax for logged in users
add_action( 'wp_ajax_nopriv_ajax_action', 'ajax_action_stuff' ); // ajax for not logged in users
?>

Here is ajaxurl code...

<?php
add_action('wp_head','pluginname_ajaxurl');
function pluginname_ajaxurl() {
?>
<script type="text/javascript">
var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
</script>
<?php
}

And here is my jquery code..

jQuery(document).ready(function(){
    jQuery('#submit').click(function(){
        var i = 0;var a = [];
        jQuery('#dashboard-page li').each(function(){
            a[i] = jQuery(this).attr('id');
            str = a.toString();
            i++;
        });
        jQuery.ajax({
            method: "POST",
            url: ajax_object.ajax_url,
            data: { action: "ajax_action", pageorder: str }
        });
    });
});
  • 写回答

2条回答 默认 最新

  • dongxing1960 2015-09-09 13:13
    关注

    I forgot to add $_POST['str'] in my update_option() function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!