我有一个在端口 3000 上运行的 React 应用程序,向端口 8080 上的 NestJS 服务器发出 POST 请求。我的客户端是:fetch('http://localhost:8080/confirm-reservation', { method: 'POST', headers: { 'Content-Type':'application/json' }, mode: 'cors', data: { key: 'this is a value' }})控制器很简单@Controller('confirm-reservation')export class AppController { @Post() postForm(@Body() form:any) { console.log(form) }}在我的主服务器上我有app.enableCors();当我提交请求时,我得到一个空的{}我可以使用具有与浏览器相同标头的 Postman 发出请求,并且能够在服务器中看到 req 对象,但不能通过使用 fetch 来查看。为什么是这样?
1 回答

蓝山帝景
TA贡献1843条经验 获得超7个赞
使用body选项对象中的属性,没有data属性。然后以 JSON 形式发送数据。
fetch('http://localhost:8080/confirm-reservation', {
method: 'POST',
headers: {
'Content-Type':'application/json'
},
mode: 'cors',
body: JSON.stringify({
key: 'this is a value'
})
})
添加回答
举报
0/150
提交
取消