【问题标题】:Drupal Ajax callback nullDrupal Ajax 回调 null
【发布时间】:2012-08-22 06:43:15
【问题描述】:

我正在尝试通过 Jquery 和 AJAX 处理页面数据。我想向 Drupal 发送一个字符串,附加另一个字符串,然后将其发送回客户端。我收到“Uncaught TypeError: Cannot read property 'messageLog' of null”错误

这是我目前所拥有的。感谢您的帮助:

Drupal 模块:

function exoticlang_chat_log_init(){
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system', 'drupal.ajax');
}

function exoticlang_chat_log_permission() {
   return array('access ExoticLang Chat Log');
}

/**
 * Implementation of hook_menu().
 */
function exoticlang_chat_logger_menu() {
    $items = array();
    $items['chatlog'] = array(
        'title'            => t('ChatLog'),
        'type'             => MENU_CALLBACK,
        'page callback'    => 'exoticlang chat log ajax',
        'access_callback' => 'user_access',
        'access arguments' => array('save chat log')
    );
    return $items;
}

function exoticlang_chat_log_ajax($logData){
    $messageLog= 'Drupal has processed this: '.$logData;
    ajax_deliver($messageLog);
    exit;
}

Javascript:

function sendPrivateMessageLog(privateSessionID, privateChatLog){
    var privateMessageLogJson={'user': myNick, 'chatLog': privateChatLog, 'sessionId': privateSessionID}
    privateMessageLogJson = JSON.stringify(privateMessageLogJson);
    console.log('JSONstringified: ' + privateMessageLogJson);
    jQuery.ajax({
        type: 'POST',
        url: '/chatlog',
        success: exoticlangAjaxCompleted,
        data:'messageLog=' + privateMessageLogJson,
        dataType: 'json'

    });
    return false;
}

function exoticlangAjaxCompleted(data){
    console.log('exoticlangAjaxCompleted!');
    console.log('chat log is: ' + data.messageLog);
    console.log('chat log is: ' + dump(data.messageLog));
    //console.log(dump(data));
}

【问题讨论】:

  • 您是否尝试过使用jsonp 作为dataType?您还应该能够使用 firebug 查看来自请求的完整响应。
  • 将其更改为 jsonp 并没有帮助。但是,我意识到我收到了 403 错误作为回调。我对此进行了一些研究,我唯一能找到的就是不在请求端设置 access_aruments。

标签: jquery ajax drupal-7


【解决方案1】:

@Krister Andersson,您使用 Firebug 的建议为我指明了正确的方向。我通常不使用它,因为它在 Linux 中不能很好地工作。

javascript 很好,但我不得不对 PHP 进行一些更改:

1) 在聊天记录菜单中,我删除了访问回调行。

2) 我在 items[chatlog] 页面回调中添加了下划线

3) 在exoticlang_chat_log_ajax 中,我使用了$_POST['messageLog'] 而不是将其作为函数参数传入

4) 我没有使用 drupal 的 ajax_deliver,而是简单地回显了结果。

最终模块:

<?php

function exoticlang_chat_log_init(){
drupal_add_js('misc/jquery.form.js');
drupal_add_library('system', 'drupal.ajax');
}

function exoticlang_chat_log_permission() {
  return array(
    'Save chat data' => array(
      'title' => t('Save ExoticLang Chat Data'),
      'description' => t('Send private message on chat close')
    ));
}


/**
 * Implementation of hook_menu().
 */

function exoticlang_chat_logger_menu() {
    $items = array();
    $items['chatlog'] = array(
        'type'             => MENU_CALLBACK,
        'page callback'    => 'exoticlang_chat_log_ajax',
        'access arguments' => 'Save chat data');
        //'access callback' => 'user_access');
    return $items;
}

function exoticlang_chat_log_ajax(){
    $messageLog=$_POST['messageLog'];
    $chatLog= 'Drupal has processed this: '.$messageLog;
    echo $chatLog;
    drupal_exit();
}

我希望这对将来的某人有所帮助...

【讨论】:

  • 虽然原始问题已得到解答,但我还有另一个问题 - 我无法弄清楚如何将回调中的数据获取到 Javascript 中。当我尝试向exoticlangAjaxCompleted 添加参数时 - 例如。 exoticlangAjaxCompleted(data),表示数据未定义
  • 我认为您必须将数据以json 发回,在您的exoticlang_chat_log_ajax() 函数中尝试echo json_encode(array('messageLog' =&gt; $chatLog)); 之类的东西。
猜你喜欢
  • 2021-05-07
  • 1970-01-01
  • 2018-11-13
  • 1970-01-01
  • 2017-05-16
  • 1970-01-01
  • 2017-11-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多