您的位置:首页技术文章
文章详情页

node.js - fetch跨域提交post请求,后台node接受不到ctx.request.body?

【字号: 日期:2024-07-17 08:20:14浏览:62作者:猪猪

问题描述

用fetch提交跨域的post请求,但是后台node服务根本找不到提交的数据,没有body的属性请求代码如下: let url=’http://127.0.0.1:8080/manager/user/add’; fetch(url , { method: ’POST’, headers: {}, credentials: ’credentials’, cache: ’default’, body: `projectName=dddddd ` // body: insertData, }).then((response) => { if (response.ok) {return response.json(); } }).then((json) => { let userList=JSON.stringify(json); dispatch({ type:ADD_USER, userList: userList }); }).catch((error) => { console.error(error); }); } 后台打印如下:

node.js - fetch跨域提交post请求,后台node接受不到ctx.request.body?

麻烦各位给看看!!

问题解答

回答1:

header内容要加上,以json为例子:headers: { ’Accept’: ’application/json’, ’Content-Type’: ’application/json’, }

回答2:

node获取ajax提交需要引入body-parser中间件模块。