0

我正在尝试通过 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));
}
4

1 回答 1

0

@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();
}

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

于 2012-08-23T05:55:25.157 回答