【问题标题】:Displaying Javascript variables in HTML在 HTML 中显示 Javascript 变量
【发布时间】:2017-08-10 17:32:03
【问题描述】:

我在代码 sn-ps 之后指定了下面的问题。

app.js

app.route('/conn').post(function (req,res,next) {

        var sys =require('util');
        var myPythonScript ="script1.py";
        var path =resolve("C:\\Python27\\python.exe");
        var pythonExecutable = path;

        var uint8arrayToString = function(data){
        return String.fromCharCode.apply(null, data);
        };

        const spawn = require('child_process').spawn;
        const scriptExecution = spawn(pythonExecutable, [myPythonScript]);

    // Handle normal output
    var chunk =' ';
    scriptExecution.stdout.on('data', function(data) {
        chunk +=data;
    });
    scriptExecution.stdout.on('data', function(data) {
        console.log(chunk);
    });

    // Handle error output
    scriptExecution.stderr.on('data', (data) => {
        // As said before, convert the Uint8Array to a readable string.
        console.log(uint8arrayToString(data));
    });

    scriptExecution.on('exit', (code) => {
        //console.log("Process quit with code : " + code);

    });
    res.write('<h2>chunk</h2>');
    // End data write
    //scriptExecution.stdin.end();
    });

上传.html

<html>
<form action ="/file" method ="post">

    MAC Address:<br>
  <input type="text" name="macadd" id="macadd"><br>

  Percentage:<br>
  <input type="text" name="percent" id="percent"><br>


<input type="submit"  value='Submit' id="upload">
<br>
<input type="reset" value="Reset">
</form>
<input type=button onClick="location.href='/home'" value='Home'><br>
<form action ="/conn" method= "post">
<input type="submit" value='Show Users'><br>
</form>

</html>

script1.py

import sys,time

def main():
    t=0
    while t!=5:
        t =t+1
        print t


# Start process
if __name__ == '__main__':
    main()

t 的值存储在“块”变量中,通过 console.log() 我可以在控制台上看到 t 的值。我想在upload.html 页面上显示这些值。我该怎么做呢?我尝试使用 res.render 和 res.write 函数,但没有显示任何内容。有什么办法吗?

【问题讨论】:

标签: javascript html node.js


【解决方案1】:

好吧,如果你想在 JS 中显示 HTML 中的变量,有一种简单的(ish)方法可以做到这一点。首先你可以创建元素:https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement

接下来,编辑创建的元素。在之前的步骤中,只需给它一个 id(显示在上面和下面的网站上)。 https://www.w3schools.com/js/js_htmldom_html.asp

希望有效, Ben A.K.A BlackSky

【讨论】:

    【解决方案2】:

    我使用以下代码来显示值。

    res.writeHead(200, { 'Content-Type': 'text/html' });
    res.write('<h2>Connected Users are:</h2>' + chunk);
    res.end();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-08-06
      • 2013-04-05
      • 2020-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-18
      • 2023-03-28
      相关资源
      最近更新 更多