gpt4 book ai didi

javascript - NodeJS req.body JSON 有但没有值

转载 作者:行者123 更新时间:2023-12-03 00:34:50 26 4
gpt4 key购买 nike

index.js:

var express = require('express');
var server = express();
var bodyParser = require('body-parser');

server.use(express.static('public'));
server.use(bodyParser.json());

server.post('/saveentry', (req, res) => {
console.log(req.body);
res.send("Eintrag gespeichert");
});

server.listen(80, 'localhost');

index.html:

<html>
<head>
<title>Gästebuch</title>
</head>
<body>
<div id="guestbook"></div>
<input type="text" id="entry" name="entry">
<button id="submit">Senden</button>

<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
<script>
var formData = {
entry: $("#entry").val()
}
$('#submit').click(function()
{
$.ajax({
type: 'POST',
url: '/saveentry',
data: JSON.stringify(formData),
dataType: "text",
contentType : "application/json",
success: function (data) {
console.log(data);
}
});
});
</script>
</body>
</html>

如果我按“Senden”,这是控制台中的结果:{条目:''}

如何获取表单中输入的文本?我还能尝试什么?网上没找到解决办法。

最佳答案

您应该从回调函数内的输入接收值:

$('#submit').click(function() {
var formData = {
entry: $("#entry").val()
};
$.ajax({
type: 'POST',
url: '/saveentry',
data: JSON.stringify(formData),
dataType: "text",
contentType : "application/json",
success: function (data) {
console.log(data);
}
});
});

关于javascript - NodeJS req.body JSON 有但没有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53703453/

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