0

我尝试开发一个应用程序来通过 api 在 mikrotik 用户管理器中创建用户。我还使用以下命令在终端中尝试了 JavaScript:

$node script name.js

那是有效的,并且创建了一个用户。然后我尝试通过单击 html 按钮来运行该 JavaScript。然后 JavaScript 不会运行,也没有用户创建。代码如下:

<html>
<head>

 </head>
<body>
<button type = "button" onclick="conn();">Try it</button>
<script type="text/javascript">
var api = require('mikronode');
 var connection = new api('192.168.5.1','admin','xxxxxx');
 connection.connect(function conn() {

    conn.closeOnDone(true); // All channels need to complete before the connection will close.

    var actionChannel=conn.openChannel();
    // These will run synchronsously
    actionChannel.write(['/tool/user-manager/user/add','=username=tiran','=password=123456','=customer=admin']); // don't care to do anything after it's done.
    actionChannel.write(['/tool/user-manager/user/create-and-activate-profile','=customer=admin','=numbers=tiran','=profile=general']); // don't care to do anything after it's done.
    //actionChannel.write('/tool/user-manager/user/print',function(chan) {

       //chan.on('done',function(data) {
          //packets=api.parseItems(data);
          //packets.forEach(function(packet) {
              //alert('done');
             //alert('user: '+JSON.stringify(packet));
              //console.log('user: '+JSON.stringify(packet));
          //});
          //listenChannel.close(); // This should call the /cancel command to stop the listen.
       //});
    //});
    actionChannel.close(); // The above commands will complete before this is closed.
 });
</script>
 </body>
 </html>
4

1 回答 1

2

该代码适用于 NodeJS,您不能直接在浏览器上作为 JavaScript 运行

于 2019-06-01T00:45:47.657 回答