fetch post跨域请求发送json数据,node(ctx.request.body是undefine)获取不到body?

查看:218
本文介绍了fetch post跨域请求发送json数据,node(ctx.request.body是undefine)获取不到body?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

我使用fetch发起post跨域请求,但是node后台获取不到传过来的json,但是可以获取到header,后台已经使用过body-parse,fetch post请求如下:

 let insertData={query:{data:'dfdf',message:'dfdffdf'},mutation:{data:'eeee',message:'dfdfdfge'}}
 fetch(URL, {
  method: 'POST',
  headers: {
    'Accept': 'application/json, text/javascript, */*; q=0.01',
    'Content-Type': 'application/json; charset=UTF-8'
  },
  mode: 'no-cors',
  credentials: 'credentials',
  cache: 'default',
  body:insertData
}).then((response) => {
  if (response.ok) {
    return response.json();
  }
}).then((json) => {
 
}).catch((error) => {
  console.error(error);
});

后台node服务已经使用cros设置了允许跨域,可以接受到请求,也可以获取到请求的header,但就是没有需要传过来的json数据,这是什么情况,好奇怪啊!!

解决方案

//建议先看看fetch资料,再试试

body:JSON.stringify(insertData)

这篇关于fetch post跨域请求发送json数据,node(ctx.request.body是undefine)获取不到body?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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