简体   繁体   English

快递请求正文显示为空

[英]express post request body shows up as empty

server code服务器代码

const express = require('express');
const app =  express();

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

app.post('/',(req,res)=>{
    console.log(req.body)
})

client code客户代码

const data = {idk:'stuff',idk2:'otherstuff'};

fetch('http://localhost:4000', {
    method:'POST',
    headers:{'Content-Type': 'application/json'},
    mode:'no-cors',
    body: JSON.stringify(data)
})

terminal shows: {} tried pretty much everything so help would be very much appreciated thanks in advance:)终端显示:{} 几乎尝试了所有方法,因此非常感谢您的帮助:)

You said mode: 'no-cors' which tells fetch to silently ignore anything which requires CORS permission.你说mode: 'no-cors'告诉fetch默默地忽略任何需要 CORS 权限的东西。

Setting the Content-Type to application/json requires CORS permission so your request is sent with Content-Type: text/plain instead.Content-Type设置为application/json需要 CORS 权限,因此您的请求是使用Content-Type: text/plain发送的。

This doesn't trigger the JSON body parser.这不会触发 JSON 正文解析器。

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

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