对一些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,'<').replace(/>/g,'>')
};
// 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));
内输出新行文本的函数。
params['id']
值稍后将在函数chat.addChatLine
中使用以识别正被放入聊天中的每个<div>....</div>
。
问题:价值来自哪里?javascript函数之外没有全局变量r
。从外观上看,该值似乎是null
。我是不是遗漏了什么?
代码的原始来源:http://tutorialzine.com/2010/10/ajax-web-chat-css-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/服务器端,响应在ajax.php
中通过以下行设置:
$response = Chat::submitChat($_POST['chatText']);
echo json_encode($response);
Chat::submitChat
函数位于Chat.class.php
文件中。它将所有内容插入数据库并返回数据库的行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.insertID
,开关是插入数据的数据库行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
是响应对象。
tzPost代码
$.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']
参考
- $.post
params['id'] = r.insertID;
与相同
params.id = r.insertID;
r是来自post的回调函数的输入参数。它是从服务器发回的数据。
- 从桌面读取python文件时高亮显示代码
- 如何将函数包装在函数中以避免代码重复
- 通过单击表单中的按钮,在代码生成中使用javascript生成字母数字代码
- 货币代码为欧元-金额的格式不应包含小数
- Regex代码只允许一个空格
- 如何将屏幕分辨率乘以 80%,然后在代码中使用
- 从var向代码隐藏函数传递值
- 如何动态插入jquery代码
- 我应该如何对这个简单的javascript动画进行逆向工程
- 逆向工程表格提交
- 逆向工程PHP脚本到Google Appscript (Javascript)
- 将post对象逆向工程为c#类
- Django-逆向工程管理网站's”;添加外键”;按钮
- 逆向工程Web应用程序
- 这个逆向工程算法是如何工作的
- 对一些Javascript聊天室代码进行逆向工程
- 逆向工程——如何找到Javascript保存我感兴趣的对象的位置
- 谷歌闭包生成的代码与高级设置可以逆向工程
- CSS:用于触摸屏逆向工程的hover
- Javascript逆向工程:将html表转换为字符串/文本