Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery DrupalAjax回调空_Jquery_Ajax_Drupal 7 - Fatal编程技术网

Jquery DrupalAjax回调空

Jquery DrupalAjax回调空,jquery,ajax,drupal-7,Jquery,Ajax,Drupal 7,我试图通过Jquery和AJAX处理页面数据。我想向Drupal发送一个字符串,附加另一个字符串,然后将其发送回客户端。我得到了一个“未捕获的TypeError:无法读取null属性'messageLog'的”错误 这就是我目前所拥有的。感谢您的帮助: Drupal模块: function exoticlang_chat_log_init(){ drupal_add_js('misc/jquery.form.js'); drupal_add_library('system', 'drupal.a

我试图通过Jquery和AJAX处理页面数据。我想向Drupal发送一个字符串,附加另一个字符串,然后将其发送回客户端。我得到了一个“未捕获的TypeError:无法读取null属性'messageLog'的”错误

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

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

@克里斯特·安德森,你关于使用Firebug的建议为我指明了正确的方向。我通常不使用它,因为它在Linux中工作得不太好

javascript很好,但我必须对PHP进行一些更改:

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

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

3) 在exoticlang\u chat\u log\u ajax中,我使用了$\u POST['messageLog'],而不是将其作为函数参数传入

4) 我没有使用drupal的ajax_deliver,而是简单地重复了结果

最后模块:

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

您是否尝试过使用
jsonp
作为
数据类型
?您还应该能够使用firebug查看请求的完整响应。将其更改为jsonp没有帮助。但是,我意识到回调时出现了403错误。我对此做了一些研究,唯一能找到的就是没有在请求端设置访问权限。虽然最初的问题得到了回答,但我还有一个问题——我不知道如何将回调中的数据转换成Javascript。当我尝试向exoticlangAjaxCompleted-ex.exoticlangAjaxCompleted(data)添加一个参数时,它表示数据没有定义我认为您必须将数据作为
json
发回,请尝试类似
echo json_encode(array('messageLog'=>chatLog))
exoticlang\u chat\u log\u ajax()函数中。