繁体   English   中英

逆向工程一些Javascript聊天室代码

[英]Reverse engineering some Javascript Chatroom code

我正在尝试理解某人为聊天室编写的代码。 我似乎无法绕过一条线。 这条线在做什么:

params['id'] = r.insertID;

该行代码在以下代码段的末尾:

    $('#submitForm').submit(function(){

        var text = $('#chatText').val();

        if(text.length == 0){
            return false;
        }

        if(working) return false;
        working = true;

        // Assigning a temporary ID to the chat:
        var tempID = 't'+Math.round(Math.random()*1000000),
            params = {
                id          : tempID,
                author      : chat.data.name,
                gravatar    : chat.data.gravatar,
                text        : text.replace(/</g,'&lt;').replace(/>/g,'&gt;')
            };

        // Using our addChatLine method to add the chat
        // to the screen immediately, without waiting for
        // the AJAX request to complete:

        chat.addChatLine($.extend({},params));

        // Using our tzPOST wrapper method to send the chat
        // via a POST AJAX request:

        $.tzPOST('submitChat',$(this).serialize(),function(r){
            working = false;

            $('#chatText').val('');
            $('div.chat-'+tempID).remove();//this is removing the temporary line :)

            params['id'] = r.insertID;
            chat.addChatLine($.extend({},params));
        });

        return false;
    });

我目前的理解:

上一个代码是在html页面加载后运行的。 该代码为用户提交ID为submitForm的论坛时安排了一个事件处理程序。 到现在为止还挺好。 从那里开始,进行了一些验证,最终代码开始真正调用该函数,该函数将在聊天室chat.addChatLine($.extend({},params));输出新行文本chat.addChatLine($.extend({},params));

params['id']值稍后将在chat.addChatLine函数中使用,以标识放置在聊天中的每个<div>....</div>

问题:价值从何而来? 在javascript函数之外没有全局变量r 从外观上看,该值似乎为null 我在这里想念什么吗?

代码的原始来源: http : //tutorialzine.com/2010/10/ajax-web-chat-c​​ss-jquery/

r是ajax响应对象,由tzPOST提供给回调函数。 这个tzPOST只是$.post的包装。 所以r是所请求的Web服务器的ajax响应。 一些较短版本的代码,例如:

//                                                   | here is 'r'
//                                                   | as parameter of the callback
//                                                   |
$.tzPOST('submitChat', $(this).serialize(), function(r) {
    params['id'] = r.insertID;
});

如果在scripts.js文件中搜索tzPOST函数,您将看到它仅使用jQuery的$.post函数。 开发人员使用它来为请求URL提供快捷方式和中心点:

$.tzPOST = function(action, data, callback) {
    $.post('php/ajax.php?action='+action, data, callback, 'json');
}

在php / server端,通过以下行在ajax.php设置响应:

$response = Chat::submitChat($_POST['chatText']);
echo json_encode($response);

Chat.class.php文件中可以找到Chat::submitChat函数。 它将所有内容插入数据库并返回数据库的行ID。

public static function submitChat($chatText){
    /* someother code here*/

    // save to database and get row id back
    $insertID = $chat->save()->insert_id;

    // return the insert id
    return array(
        'status' => 1,
        // this is later 'r.insertID'
        'insertID' => $insertID
    );
}

现在params['id']的值为r.insertIDwitch是插入数据的数据库行ID


为了了解r来源,我们来看看tzPOST函数中的$.post 这是jQuery $.ajax函数的别名。 因此,我们也可以使用$.ajax代替$.post

那么应该更清楚r来源。

// this is exactly the same as the one line used in 'tzPOST'
$.ajax({
    url: 'php/ajax.php?action=' + action,
    data: data,
    dataType: 'json',

    // this is a anonymous callback function
    // it gets griggered whenever the request was successfull
    //
    // the $.ajax will pass the reponse to the anonymous functions first
    // parameter, witch internally is named 'r' now
    success: function(r) {
        // so 'r' is the webservers response
    }
});

所以r只是一个内部名称,由ajax请求传递。 您可以轻松地重命名r

//                                                   | now the name is 'foo'
//                                                   |
$.tzPOST('submitChat', $(this).serialize(), function(foo) {
    params['id'] = foo.insertID;
});

正如我已经说过的, r是响应对象。

tz邮政编码

$.tzPOST = function(action, data, callback) {
  $.post('php/ajax.php?action=' + action, data, callback, 'json');
}

它在内部调用$.post ,如果您看到$.post

$.post("ajax/test.html", function(data) {
  $(".result").html(data);
});

它返回API调用的响应。

因此,以我的理解,他正在调用post来保存用户并创建一个唯一的ID,该ID作为r.post的响应返回,并设置为params['id']

参考

params['id'] = r.insertID;

是相同的

params.id = r.insertID;

r是帖子的回调函数的输入参数。 是从服务器发回的数据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM