Empty body in fetch post request

前端 未结 4 1414
你的背包
你的背包 2020-12-15 23:10

i\'m struggling with the fetch API in javascript. When i try to POST something to my server with fetch, the request body an empty array. But when i use Postman it works... H

4条回答
  •  余生分开走
    2020-12-15 23:22

    To complement the excellent answers that mentioned removing the mode: 'no-cors' option, and if you don't feel like adding a CORS middleware to Express, you may just want to handle "pre-flight" requests under an OPTIONS block:

    app.options('*', (req, res) => {
      res.setHeader('Access-Control-Allow-Origin', '*');
      res.setHeader('Access-Control-Allow-Headers', 'Content-Type'); // Add other headers here
      res.setHeader('Access-Control-Allow-Methods', 'POST'); // Add other methods here
      res.send();
    });
    

    Another nice answer here : https://www.vinaygopinath.me/blog/tech/enable-cors-with-pre-flight/

    Hope this helps!

提交回复
热议问题