当ADD_ACTION在Functions.php中的类内时,AJAX调用返回0

时间:2015-09-16 作者:fernandus

Ajax调用返回0作为输出。我确信钩子不起作用,它不会调用test2 方法为什么是add_action(... 不在班内工作functions.php?

--------包裹内部functions.php 开始-------

class test{
    public function __construct() {
        add_action( \'wp_ajax_test2\', array( $this, \'test2\' ) );
    }                       
    public function test1() { ?>
        <script>
            jQuery(document).ready(function($){
                var ajaxurl = "<?php echo get_site_url();?>/wp-admin/admin-ajax.php";
                var data = {\'action\':\'test2\'};
                 $.ajax({   
                    url: ajaxurl,
                    type: "POST",
                    data: data,
                    success: function(val) {
                        alert(val);
                    },
                }); 
            });             
        </script><?php 
    }               
    public function test2(){
        echo "success";
        exit;
    }       
}
----------内部包裹functions.php 结束-------

在模板页中创建了一个对象并调用test1方法:

$ob_call = new test;
$ob_call->test1();

3 个回复
SO网友:Prasad Nevase

虽然我不知道您为什么要这样做,但请查看下面的工作代码。请注意行中的注释。我希望这有帮助。


class test{
    public function __construct() {
        add_action( \'wp_ajax_test2\', array( $this, \'test2\' ) );
        /* Front end ajax needs this. */
        add_action( \'wp_ajax_nopriv_test2\', array( $this, \'test2\' ) );
        add_action( \'wp_enqueue_scripts\', array( $this, \'test1\' ) );
    }

    public function test1() {
      /* in JavaScript, object properties are accessed as ajax_object.ajax_url, ajax_object.we_value */
      wp_localize_script(\'test-script-ajax\', \'ajaxobj\', array(\'ajaxurl\' => admin_url(\'admin-ajax.php\')));
      /* Moved your js to a separate js file and enquing it the WordPress way */ 
      wp_enqueue_script( \'test-script-ajax\', get_stylesheet_directory_uri() . \'/js/test-ajax.js\', array( \'jquery\' ));
    }

    public function test2(){
        echo "success";
        exit;
    }
}

$ob_call = new test;
$ob_call->test1();

下面是测试ajax的代码。js文件


jQuery(document).ready(function($){
  var data = { \'action\':\'test2\' };
  jQuery.post( ajaxobj.ajaxurl, data, function( response ) {
            alert(response);
    });
});

SO网友:fernandus

现在可以了

----------Function.php---------------

add_action( \'wp_ajax_test2\', array( \'test\', \'test2\' ) );

class test{

    public function __construct() {
        
    }                       
    public function test1() { ?>
        <script>
            jQuery(document).ready(function($){
                var ajaxurl = "<?php echo get_site_url();?>/wp-admin/admin-ajax.php";
                var data = {\'action\':\'test2\'};
                 $.ajax({ 
                    url: ajaxurl,
                    type: "POST",
                    data: data,
                    success: function(val) {
                        alert(val);
                    },
                }); 
            });             
        </script><?php 
    }               
    public function test2(){
        echo "success";
        exit;
    } 
          
}
<人力资源/>--------------第页(Template)-------------

$ob_call = new test;

$ob_call->test1();
看看https://wordpress.stackexchange.com/questions/203383/why-hooking-differs-in-plugin-and-function-php-wordpress

SO网友:Ayoub Bousetta

添加wp_die(); 在功能结束时。

相关推荐

WordPress AJAX错误400向远程站点发送数据的错误请求

我正在使用发件人。net获取电子邮件订阅列表。这个网站给了我一些信息,可以将用户的电子邮件添加到订阅列表中。我想使用WordPress ajax来实现这一点。但它返回错误400错误请求。我的代码是:文件ajax新闻脚本。js公司: jQuery(document).ready(function($){ // Perform AJAX send news on form submit $(\'form#fnews\').on(\'submit\', funct