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

我想知道你是否能在这件事上帮助我。


调用函数:


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”对象中。


弑天下
浏览 78回答 1
1回答

跃然一笑

缺少标题,试试这个..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}`);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript