加菲大帅猫 2023-03-17 10:14 采纳率: 35%
浏览 40
已结题

node.js文件怎么接收前端页面传过来的值,和前端怎么给node.js传值,目前我可以做到从node.js取到值

node.js文件怎么接收前端页面传过来的值,和前端怎么给node.js传值,目前我可以做到从node.js取到值,但是不能由前端传值给后端
这是前端的请求

axios({
        url: "http://localhost:3000/api/table",
        method: "GET"     
      }).then((ok) => {
        console.log(ok.data.data.list)
        for (let i = 0; i < ok.data.data.list.length; i++) {
          if(ok.data.data.list[i][0]!=0){
            this.DeviceAlarmData.push({
            clock: "Core",
            severity: ok.data.data.list[i][1],
            r_clock: "System Error",
            r_eventid: this.SnmpTime(ok.data.data.list[i][2]),
            IPname: ok.data.data.list[i][4]
          })
          }
          
        }
        console.log(this.DeviceAlarmData)

  }).catch((err) => {
    console.log(err)
  })


这是node.js的代码


app.get('/api/table', (req, res) => {
console.log(req)
    let oid = "1.3.6.1.4.1.52126.1.1.2.1";
    let session = snmp.createSession("192.168.3.155", "txwrite");
    let maxRepetitions = 20;
    session.table(oid, maxRepetitions, function(error, table) {
        if (error) {
            console.error(error.toString());
        } else {
            // This code is purely used to print rows out in index order,
            // ifIndex's are integers so we'll sort them numerically using
            // the sortInt() function above
            var indexes = [];

            for (index in table)
                indexes.push(parseInt(index));
            indexes.sort(sortInt);

            // Use the sorted indexes we've calculated to walk through each
            // row in order
            let arr = []
            for (var i = 0; i < indexes.length; i++) {
                // Like indexes we sort by column, so use the same trick here,
                // some rows may not have the same columns as other rows, so
                // we calculate this per row
                var columns = [];
                for (column in table[indexes[i]])
                    columns.push(parseInt(column));
                columns.sort(sortInt);
                arr.push([])
                for (var j = 0; j < columns.length; j++) {
                    arr[i].push("" + table[indexes[i]][columns[j]])
                }

                // Print index, then each column indented under the index
                console.log("table " + table)
            }
            res.json({
                code: 200,
                message: '成功',
                data: {
                    list: arr
                }
            });
        }
        session.close();
    });

})

  • 写回答

3条回答 默认 最新

  • 追cium 2023-03-17 10:20
    关注

    参考GPT和自己的思路:

    要让前端页面能够向 Node.js 传值,可以使用 POST 或 PUT 等 HTTP 请求方法,具体实现可以使用 Express 的 app.post()app.put() 方法来处理前端发来的数据。

    例如,你可以在前端使用 axios.post() 方法来发送一个 POST 请求,请求数据可以以 Object 或者 FormData 等形式发送。

    Node.js 中,你也需要使用 body-parser 模块来解析前端发送的数据,然后进行处理。具体实现可以参照如下代码示例:

    前端代码:

    axios({
      method: 'post',
      url: '/data',
      data: {
        name: 'vue.js',
        author: 'Evan You'
      }
    })
    

    Node.js 代码:

    const bodyParser = require('body-parser');
    const express = require('express');
    const app = express();
    
    app.use(bodyParser.json()); // 解析 application/json 类型数据
    app.use(bodyParser.urlencoded({ extended: true })); // 解析 application/x-www-form-urlencoded 类型数据
    
    app.post('/data', (req, res) => {
      console.log(req.body); // { name: 'vue.js', author: 'Evan You' }
      res.send('success');
    });
    
    app.listen(3000, () => {
      console.log('server started');
    });
    

    以上代码示例中,app.use(bodyParser.json()) 方法用于解析 POST 请求中的 JSON 格式的数据,app.use(bodyParser.urlencoded({ extended: true })) 方法用于解析 POST 请求中的 URL 编码形式的数据。app.post() 方法用于接收前端发送的 POST 请求,并且可以获取请求中的数据 req.body,然后进行处理。最后,使用 res.send() 方法给前端发送一个响应结果。

    如果使用 GET 请求来向 Node.js 传递参数,前端页面可以使用 QueryString 或 RESTful API 的形式传递参数,具体实现方法类似,需要在 Node.js 中通过 req.queryreq.params 来获取参数数据。

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 3月22日
  • 创建了问题 3月17日

悬赏问题

  • ¥15 易优eyoucms关于二级栏目调用的问题
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题
  • ¥50 如何将脑的图像投影到颅骨上
  • ¥15 提问一个关于vscode相关的环境配置问题,就是输入中文但是显示不出来,代码在idea可以显示中文,但在vscode不行,不知道怎么配置环境
  • ¥15 netcore使用PuppeteerSharp截图
  • ¥20 这张图页头,页脚具体代码该怎么写?