【发布时间】:2021-03-19 17:34:05
【问题描述】:
我正在检索一个发布请求正文,以便我可以将正文中的参数添加到我的数据库中。
起初我收到一个错误 SyntaxError: Unexpected token ' in JSON at position 0 at JSON.parse (<anonymous>) 但我添加了这一行 applctn.use(express.urlencoded({ extended: true })); // to support URL-encoded bodies 现在我得到一个空的正文。
我的代码如下所示:
const express = require("express");
const moment = require("moment");
const db = require("./dbconnection.js"); //reference of dbconnection.js
var bodyParser = require("body-parser");
const applctn = express();
applctn.use(bodyParser.urlencoded({extended: true}));
applctn.post("/hl7_message", function(req, res) {
var jsonObj = JSON.stringify(req.body);
console.log(req);
当我添加 ```app.use(bodyParser.json()); // 支持 JSON 编码的正文
```SyntaxError: Unexpected token ' in JSON at position 0 at JSON.parse (<anonymous>)
任何关于我如何检索正文内容的建议/提示将不胜感激。
【问题讨论】:
-
尝试使用
applctn.use(express.json())这可能是您的设置中缺少的东西。 -
@dhruv479,当我补充说我得到这个错误
SyntaxError: Unexpected token ' in JSON at position 0 at JSON.parse (<anonymous>)
标签: javascript node.js express