简体   繁体   English

无法从POST请求检索JSON数据

[英]Can't retrieve JSON data from POST request

I'm making a simple Node.js web server and I have troubles with sending requests to the server. 我正在制作一个简单的Node.js Web服务器,但在向服务器发送请求时遇到了麻烦。 Here's an example with a simple html "form". 这是一个带有简单html“ form”的示例。 I send a stringified JSON data, but the req.body is empty. 我发送了一个字符串化的JSON数据,但是req.body为空。

HTML: HTML:

<!DOCTYPE html>
<html lang="ru" dir="ltr">
    <head>
        <meta charset="utf-8">
    </head>
    <body>

        <input type="text" id="field" value="foo">
        <button type="button" id="button">bar</button>

        <script>
            addElement = function()  {
                var xhr = new XMLHttpRequest();
                xhr.open('POST', '/database');
                xhr.setRequestHeader('contentType', 'application/json; charset=utf-8');

                var newElem = new Object();
                newElem.name = field.value || "";
                var requestData = JSON.stringify(newElem);
                console.log(newElem);
          // {name: "foo"}
                xhr.send(requestData);
            }

            button.onclick = addElement;
        </script>
    </body>
</html>

Node Server code: 节点服务器代码:

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var PORT = process.env.PORT || 3000;

app.use(bodyParser.json());

app.get("/", function(req,res){
    res.sendFile(__dirname +"/index.html");
});

app.post('/database', function(req, res) {
    console.log("req.body = ", req.body);

//expected output: req.body = ({name: foo})
//actual output: req.body = ()

});

app.listen(PORT, function() {
    console.log('Server listening on ' + PORT);
});

Replace your contentType to Content-Type . 将您的contentType替换为Content-Type Hope it helps 希望能帮助到你

You need to change contentType to Content-Type . 您需要将contentType更改为Content-Type

Mozilla Content-Type Docs Mozilla内容类型文档

if you want to send the request to server. 如果要将请求发送到服务器。 first u need to add the server Url. 首先,您需要添加服务器网址。

for Example 例如

In case your server application is running in 3000 means then u change 如果您的服务器应用程序以3000的速度运行,则您进行更改

xhr.open('POST', '/database'); xhr.open('POST','/ database'); to xhr.open('POST', ' http://localhost:3000/database '); 到xhr.open('POST',' http:// localhost:3000 / database ');

the following code has : 以下代码具有:

 <!DOCTYPE html> <html lang="ru" dir="ltr"> <head> <meta charset="utf-8"> </head> <body> <input type="text" id="field" value="foo"> <button type="button" id="button">bar</button> <script> addElement = function() { var xhr = new XMLHttpRequest(); xhr.open('POST', 'http://localhost:3000/database'); xhr.setRequestHeader('contentType', 'application/json; charset=utf-8'); var newElem = new Object(); newElem.name = field.value || ""; var requestData = JSON.stringify(newElem); console.log(newElem); // {name: "foo"} xhr.send(requestData); } button.onclick = addElement; </script> </body> </html> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM