-3

我正在研究 jQuery 终端仿真器插件:https ://terminal.jcubic.pl/但在文档中找不到我要查找的内容,实际上发现它有点太高级了,我无法理解,所以我在这里询问如何我会使用插件从 PHP 中的 json 对象加载命令和参数回复,还是完全加载单独的文件?

4

1 回答 1

1

您需要做的事情与您对普通 ajax 应用程序所做的事情完全相同。您需要发送 ajax 请求 POST 或 GET 并解析响应,然后回显结果(使用 jQuery 终端而不是交换 html 或其他东西)。并且需要在函数中作为第一个参数 t jquery 终端触发 ajax 请求,因此您可以获得原始命令数据。

$(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'],检查我的皮带壳的源代码。

如果您在具有映射的文件中有 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]);
        }
    });
});

如果你也有论据,你将需要更复杂的逻辑,

于 2018-02-07T08:03:33.897 回答