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条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?