【发布时间】:2018-02-07 04:44:24
【问题描述】:
我正在研究 jQuery 终端仿真器插件:https://terminal.jcubic.pl/,但在文档中找不到我要查找的内容,实际上发现它有点太高级了,我无法理解,所以我在这里询问我将如何使用用于从 PHP 中的 json 对象或完全单独的文件加载命令和参数回复的插件?
【问题讨论】:
标签: javascript php jquery json jquery-terminal
我正在研究 jQuery 终端仿真器插件:https://terminal.jcubic.pl/,但在文档中找不到我要查找的内容,实际上发现它有点太高级了,我无法理解,所以我在这里询问我将如何使用用于从 PHP 中的 json 对象或完全单独的文件加载命令和参数回复的插件?
【问题讨论】:
标签: javascript php jquery json jquery-terminal
您需要执行与使用普通 ajax 应用程序完全相同的操作。您需要发送 ajax 请求 POST 或 GET 并解析响应,然后回显结果(使用 jQuery 终端而不是交换 html 或其他东西)。并且需要在函数中触发 ajax 请求作为 jquery 终端的第一个参数,因此您可以获得原始命令数据。
$(function() {
$('body').terminal(function(command, term) {
$.post('script.php', {command: command}, function(response) {
// response is already parsed by jQuery
if (response.output) {
term.echo(response.output);
}
// you can do other things with other values, like execute
// terminal methods
if (response.exec) {
term[response.exec.method].apply(term, response.exec.args);
}
}, 'json');
}, {
greetings: 'php example',
onBlur: function() {
return false;
}
});
});
在php中你需要使用这样的代码:
<?php
if (isset($_POST['command'])) {
// process command
echo json_encode($arrayorobject);
}
?>
如果你想拥有与服务器和客户端相同的文件,你可以使用 $_SERVER['HTTP_X_REQUESTED_WITH'] 的技巧,查看我的 leash shell 的源代码。
如果您的文件中有 json 对象,该文件具有映射 {command: reply},您可以这样做:
$.get('commands.json', function(commands) {
$('body').terminal(function(command, term) {
var cmd = $.terminal.parse_command(command)
if (commands[cmd.name]) {
this.echo(commands[cmd.name]);
}
});
});
如果你也有论据,你将需要更复杂的逻辑,
【讨论】: