为了账号安全,请及时绑定邮箱和手机立即绑定

请求正文未从 Next.js 传递到 Node.js 服务器

请求正文未从 Next.js 传递到 Node.js 服务器

弑天下 2023-09-28 15:23:49
我想知道你是否能在这件事上帮助我。调用函数:function apiCreate(url, product) {console.log('Posting request API...' + JSON.stringify(product) );fetch(url, {    dataType: 'json',    method: 'post',    body: product}).then(function(response) {    console.log("we did it")  return response.json();}).then(function(data) {    console.log('Created Gist:', data.html_url);});接收的是:router.post('/create', (req, res) => {    console.log('catalog product create Called.');    console.log('addProduct Called..');    console.log('req: '+ JSON.stringify(req.body));我可以到达终点,但主体不在“req”对象中。
查看完整描述

1 回答

?
跃然一笑

TA贡献1826条经验 获得超6个赞

缺少标题,试试这个..


function apiCreate(url, product) {

      try {

        fetch(url, {

            method: 'POST',

            headers: {

              'Accept': 'application/json',

              'Content-Type': 'application/json'

            },

            body: JSON.stringify(product)

        })

        .then(response => response.json())

        .then(data => console.log(data))

        .catch(error => console.log(error))  

      } catch (err) {

        console.log(err)

      }

}

接收...


router.post('/create', (req, res) => {

    console.log('catalog product create Called.');


    console.log('addProduct Called..');

    console.log(`req ${body.req}`);


查看完整回答
反对 回复 2023-09-28
  • 1 回答
  • 0 关注
  • 45 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信