Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json 请求内容类型和emty请求正文_Json_Express - Fatal编程技术网

Json 请求内容类型和emty请求正文

Json 请求内容类型和emty请求正文,json,express,Json,Express,早上好,这是我的服务器配置: app.use(cors()); app.use(bodyParser.text()); app.use(bodyParser.json()); // parse application/vnd.api+json as json app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/x-www-form-urlencoded app.use(bod

早上好,这是我的服务器配置:

app.use(cors());
app.use(bodyParser.text());
app.use(bodyParser.json());

// parse application/vnd.api+json as json
app.use(bodyParser.json({ type: 'application/vnd.api+json' }));

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true })); //true

// override with the X-HTTP-Method-Override header in req
app.use(methodOverride('X-HTTP-Method-Override'));
在浏览器中使用API时,请求内容类型设置为text/plain,并返回500内部服务器错误。当我做一个

console.log(req.body) 
它是空的,当我使用HTTP请求程序测试它并将内容类型设置为“application/json”时,它正常工作。
我很困惑,希望您能帮助我。

这是因为您只有配置主体解析器来解析JSON和URL编码的数据。。。如果你想阅读文本/纯文本,你必须添加
app.use(bodyParser.text())正如这里所说:

谢谢你的快速回答,我添加了它,但仍然会出现错误。你可以编辑你的问题以显示整个脚本吗?请(不要犹豫,使用triple`来编写代码块)console.log解析请求主体,现在返回请求主体,但随后显示此错误无法读取req.body中未定义的属性'UDID'(我不明白,我的意思是它读取req.body,这不是emty为什么未定义)您的请求中的udid属性没有设置,或者您可以在脚本中使用它,但没有设置。当我重新启动服务器时,它准确地再次显示相同的错误,这是请求主体:{“initObj”:{“udid”:“1256”},它在console.log(req.body)中显示相同的内容,但我执行了console.log(req.body.initObj)它返回未定义抱歉,但我真的不知道问题出在哪里