wp_ajax_[service] returning 0

时间:2012-09-08 作者:ken

我正在设置一些Ajax服务。所有这些都需要登录,所以我将挂钩连接到了“wp\\uajax\\u[$服务]”。问题是,当我调用这些服务时,只得到一个“0”响应。我的php错误日志中也没有任何记录。

我想知道的是:

有没有办法钩住所有Ajax请求并将一些调试信息转储到日志文件中,这样我就可以了解更多的信息更新:

包括代码以提供更多上下文。首先是Javascript,它是:

ServiceCall ( \'get-action-template\' , {\'action\' : action} , function(response) {// callback function});
ServiceCall函数为:

function ServiceCall ( service , parameters , callback ) {
    console.log (\'getTemplate was called: (\' + service +\', \' + parameters + \')\' );

    //initialise a request object
    var requestObj = {};
    requestObj.response = \'ajax-response\';
    requestObj.type = \'POST\';
    requestObj.url =  my_config[\'ajax-service-list\'][service][\'url\'];
    requestObj.data = $.extend ( requestObj.data , {
                                            \'service\': service,
                                            \'request_parameters\': JSON.stringify(parameters),
                                            \'ajax_nonce\': my_config[\'ajax-service-list\'][service][\'nonce\']
    });
    requestObj.global = false;
    requestObj.timeout = 30000;
    requestObj.success = function ( r ) {
        alert ( "Success: " + JSON.stringify (r) );
        callback ( r );
    }
    requestObj.error = function ( r ) {
        console.log ("FAILURE WITH AJAX Call ( " + JSON.stringify (r) + ")");
    }
    $.ajax( requestObj );
}
然后生成以下消息(取自浏览器调试器中的网络面板):

debug

最后,这里是处理程序函数的代码(从未被调用):

public static function ajax_handler () {
    error_log ( "AJAX HANDLE"  ); 
    $service = $_POST[\'action\'];
    $request_parameters = json_decode ($_POST[\'request_parameters\']);
    if ( LG_Utility::key_from_hash ( self::$services_list , $service , false ) ) {
        $object = new self::$services_list[$service][\'class\'] ($service);
        $object->handle( $service , $request_parameters );
    } else {
        if (class_exists (\'AppLogger\') ) AppLogger::error ( "Trying to handle a service ({$service}) which is not registered!"); 
    }
}
但是,我已经验证了它是否正确连接到wp\\u ajax\\u get-action-template事件。简写的“证明”是使用流行的“调试对象”插件,我能够验证它是否已设置。下面是管理AJAX事务的整个PHP类(为了简洁起见,删除了一些方法/常量):

 class LG_AjaxServiceManager
 {
   private static $services_list = array(
    \'get-action-template\' =>    array ( \'class\' => \'LG_GetActionTemplate\' , \'login_required\' => true ),
    \'some-other-service\' =>     array ( \'class\' => \'LG_FooBar\' , \'login_required\' => false )
   );
   public static function add_listeners () {
    foreach ( self::$services_list as $id => $service ) {
        add_action ( \'wp_ajax_\' . $id , \'LG_AjaxServiceManager::ajax_handler\' );
        if (class_exists (\'AppLogger\') ) AppLogger::log ( "Registering $id as an Ajax service" ); 
    }   
   } 
   public static function ajax_localize () {
    // add hook for script localisation; this is important so that JS scripts have nonce and URL information
    foreach ( self::$services_list as $id => $service ) {
        $service_url = wp_nonce_url ( admin_url ( \'admin-ajax.php?action="\' . $id .\'"\' ) , self::AJAX_APP_NONCE_KEY . \'_\' . $id );
        preg_match ( \'/.*action=%22(.*)%22&_wpnonce=(.*)/\' , $service_url , $service_parts );
        self::$ajax_js_array [ $service_parts[1] ] = array ( \'nonce\' => $service_parts[2] , \'url\' => esc_url ( $service_url ) );
    }

    $localize = array (
            \'SiteURL\' => get_bloginfo (\'url\'),
            \'AjaxURL\' => admin_url (\'admin-ajax.php\'),
            \'ajax-service-list\' => self::$ajax_js_array
    );
    wp_enqueue_script ( \'jquery\' );
    wp_enqueue_script ( self::JS_FILE_HANDLE  , plugins_url ( \'/js/lg-ajax-client.js\' , dirname(__FILE__)) , \'jquery\' );

    wp_localize_script( self::JS_FILE_HANDLE , self::JS_NAMESPACE , $localize );

   }
   public static function ajax_handler () {
    error_log ( "AJAX HANDLE"  ); 
    $service = $_POST[\'action\'];
    $request_parameters = json_decode ($_POST[\'request_parameters\']);
    if ( LG_Utility::key_from_hash ( self::$services_list , $service , false ) ) {
        $object = new self::$services_list[$service][\'class\'] ($service);
        $object->handle( $service , $request_parameters );
    } else {
        if (class_exists (\'AppLogger\') ) AppLogger::error ( "Trying to handle a service ({$service}) which is not registered!"); 
    }
   }
}

2 个回复
SO网友:user123

根据WordPress documentation, 你应该打电话die() 在PHP函数的末尾返回正确的结果。

SO网友:Byron

为了便于记录。我花了两天时间解决了一个ajax问题,今天下午开始测试时,我的PHP ajax处理程序没有被调用。。。。

非常令人沮丧。

解决我的问题(可能会帮助他人)的方法是priorityadd_action 回调:

add_action( "wp_ajax_{$my_handler}", array(\'class_name\', \'static_function\'), 1);
回顾违约priority = 10

我得到的返回代码为零,但没有调用任何代码。

结束