gpt4 book ai didi

javascript - 如何在 Node 中解析 POST 响应体

转载 作者:行者123 更新时间:2023-11-30 13:51:04 25 4
gpt4 key购买 nike

    
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
app.get(bodyParser.urlencoded({extended: true}));
app.get("/", function(req, res){
res.sendFile(__dirname + "/index.html");
});
app.post("/", function(req, res){
console.log(req.body.crypto);
});
app.listen(3000, function(){
console.log("server is running in port 3000");
});

    
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
app.get(bodyParser.urlencoded({extended: true}));
app.get("/", function(req, res){
res.sendFile(__dirname + "/index.html");
});
app.post("/", function(req, res){
console.log(req.body.crypto);
});
app.listen(3000, function(){
console.log("server is running in port 3000");
});
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
<meta charset="utf-8">
<title>Bitcoin ticker</title>
</head>

<body>
<h1>Bitcoin Ticker</h1>

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

<select name="crypto">
<option value="BTC">Bitcoin</option>
<option value="ETC">Ethereum</option>
<option value="LTC">Litecoins</option>
</select>

<select name="fiat">
<option value="USD">US Doller</option>
<option value="GBP">GB Pounds</option>
<option value="EUR">EU Euroes</option>
</select>

<button type="submit" name="button">Check</button>
</form>

</body>

</html>

当 iam 请求发布请求时,javascript 代码出现问题,浏览器显示无法读取未定义的属性“crypto”当我运行代码时没有错误但是当我选择时无法读取属性(property)“加密货币”我认为错误可能在这里console.log(req.body.crypto);

最佳答案

您的req.body 未定义,

你必须做这条线

app.get(bodyParser.urlencoded({extended: true}));

app.use(bodyParser.urlencoded({extended: true}));

关于javascript - 如何在 Node 中解析 POST 响应体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58231157/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com