从React开始,js作为GET到达node/js服务器

eaf3rand  于 2023-04-29  发布在  Node.js
关注(0)|答案(1)|浏览(108)

似乎我的POST请求正在REACT客户端代码中的某个地方转换为GET。这是react代码:

function SavePatient(event) {

  event.preventDefault();
  console.log("Saving Patient Data");
  console.log(patientData);

  fetch('http://localhost:3001',
  {
    Method: 'POST',
    Headers: {
      'Accept': 'application/json',
      'Content-Type': 'multipart/form-data'
    },
    Body: JSON.stringify(patientData),
    Cache: 'default'
  });

这是服务器代码:

function requestListener(req,res) {

    res.setHeader('Access-Control-Allow-Origin', '*'); 
    res.setHeader( 'Access-Control-Allow-Headers',
                    'Origin, X-Requested-With, Content-Type, Accept');

    console.log(req.url,req.method,req.headers);
    // console.log(req);
    //process.exit();

    const url = req.url;
    const body = [];
    let parsedBody = "";

    function readData(chunk) {
        console.log(chunk);
        body.push(chunk);
    }

    function endReadData(chunk) {
        parsedBody = Buffer.concat(body).toString();
        console.log(parsedBody);
    }

    if (url === '/savepatient') {
        const body = [];
        req.on('data', readData);
        req.on('end', endReadData); 
        res.setHeader('Content-Type', 'text/json');
        res.setHeader('Access-Control-Allow-Origin', '*'); 
        res.setHeader( 'Access-Control-Allow-Headers',
                        'Origin, X-Requested-With, Content-Type, Accept');
        res.write('{"name":"John", "age":30, "car":null}');
        console.log('Saving...');
        fs.writeFileSync('storage/message.txt','TESTE');
        return res.end();
    }
    // console.log('Aqui')
    // res.setHeader('Content-Type', 'text/html');
    // res.write('<html>');
    // res.write('<head><title>MEDPRO.app server</title></head>');
    // res.write('<body><h1>Hello from the MEDPRO.app server!</h1></body>');
    // res.write('</html>');
    // res.end();
}

服务器代码工作正常,我使用postman接收POST请求。问题是,当我使用fetch从客户端发送请求时。它以POST的形式到达。很奇怪
我期望POST请求到达服务器。
用postman works find测试服务器。
对于我的客户端,这是服务器接收到的消息:
/ GET { host:'localhost:3001',连接:'keep-alive','sec-ch-ua':'“ chrome ”;v=“112”,“Google Chrome”;v=“112”,“非:A-品牌”;v=“99”','sec-ch-ua-mobile':'?0',' user-agent ':'Mozilla/5.0(Windows NT 10。0; Win64; x64)AppleWebKit/537。36(KHTML,like Gecko)Chrome/ www.example.com .36',
'sec-ch-ua-platform':“Windows”,接受:'/',来源:'http://localhost:3000','sec-fetch-site':'same site','sec-fetch-mode ':'cors','sec-fetch-dest':“空”,referer:'http://localhost:3000 /',' accept-encoding':'gzip,deflate,br','accept-language':en,en-US;q=0。q= 0.8,pt-BR;q= 0.7,pt;q=0.6' }

bf1o4zei

bf1o4zei1#

尝试在fetch方法中将所有内容都设置为小写;这应该可以解决这个问题,因为它是区分大小写的。
就像这样:

function SavePatient(event) {

  event.preventDefault();
  console.log("Saving Patient Data");
  console.log(patientData);

  fetch('http://localhost:3001',
  {
    method: 'POST',
    headers: {
      'Accept': 'application/json',
      'Content-Type': 'multipart/form-data'
    },
    body: JSON.stringify(patientData),
    cache: 'default'
  });

相关问题