WP插件中的AJAX始终返回0 [英] AJAX in WP Plugin returns 0 always

查看:130
本文介绍了WP插件中的AJAX始终返回0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的在WP主题中成功实现后,就可以在Under-dev插件中.

I'm trying to implement WP AJAX in one of my Under-dev Plugin after successful implementation in WP Theme.

function deactivate_ad() {

    if( isset( $_POST['id'] ) ) {
        echo $_POST['id'];
        echo 'Deactivated';
        die;
    } else {
        echo 'Sorry, not done';
        die;
    }
    wp_die(); // ajax call must die to avoid trailing 0 in your response
}

add_action('wp_ajax_deactivate_ad', 'deactivate_ad');
//add_action( 'wp_ajax_nopriv_deactivate_ad', 'deactivate_ad'); //not logged in users

在函数的下面,我添加了带有动态ID的表单:

And beneath the function somewhere, I added my form with dynamic ID:

<form action="" enctype="multipart/form-data" method="post">
          <script type="text/javascript">
          var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';

          jQuery(document).on('click', '.deactivate-ad', function () {
              var id = this.id;
              alert(id);
              jQuery.ajax({
                  type: 'POST',
                  url: ajaxurl,
                  data: {
                    "action": "deactivate_ad",
                    "id": id
                  },
                  success: function (data) {
                    alert(data);
                  }
              });
          });
        </script>
<?php
        //after the db Query, within the foreach loop
        foreach( $ad_query as $the_ad ){
            $element_id = 'deactivate_' . $the_ad->id . '_' . wp_create_nonce('deactivate_' . $the_ad->id );
            // I'm just checking with the 'Deactivate' button first
            echo ( $the_ad->ad_status == 1 ? '<a class="deactivate-ad" href="#" id="'. $element_id .'">Deactivate</a>' : '<a href="?page=site-ad&id='. $the_ad->id .'&activate=true&success=true">Activate</a>' );
        }
?>
</form>

我只是想激活AJAX,以便随后我可以做任何我想做的事情.但是使用第一个alert(id)时,我可以成功地用随机数提醒id,但是alert(data)总是返回0(零).

I'm just trying to activate the AJAX so that I can do whatever I want then. But with first alert(id) I can alert the id with nonce successfully, but the alert(data) it's always returning 0 (zero).

我正在使用最新的jQuery( http://code.jquery.com/jquery -latest.min.js ),并检查了所有实例.这次,我在做什么错-我获得的过程在一个主题下起作用-我以前工作过.

I am enqueueing the latest jQuery (http://code.jquery.com/jquery-latest.min.js) at the beginning and checked all the instances. What am I doing wrong this time - the process I acquired worked in a theme - I worked earlier.

推荐答案

您应将以下代码从adzonia-view.php替换为wp-adzonia.php

You should re-place the following code from adzonia-view.php to wp-adzonia.php

function deactivate_ad() {

if( isset( $_POST['id'] ) ) { echo $_POST['id']; echo 'Deactivated';

} else { echo 'Sorry, not done';

} die();

}

add_action('wp_ajax_deactivate_ad', 'deactivate_ad');

原因:
因为您在菜单页面中编写了功能.它已经被钩住了,可能是admin_menu钩子.因此在wp_ajax_*中添加动作为时已晚.

Reason:
Because you wrote the functions in a menu page. It's already hooked in with may be admin_menu hook. So it's too late to add actions in wp_ajax_*.

这篇关于WP插件中的AJAX始终返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆