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

查看:227
本文介绍了node.js - fetch跨域提交post请求,后台node接受不到ctx.request.body?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

用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);  
}); 
    }
   后台打印如下: 

麻烦各位给看看!!

解决方案

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

这篇关于node.js - fetch跨域提交post请求,后台node接受不到ctx.request.body?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆