来自react.js的POST以GET形式到达node/js服务器
P粉316423089
P粉316423089 2024-04-03 17:22:04
0
1
338

看来我的 POST 请求正在我的 REACT 客户端代码中的某处转换为 GET。这是反应代码:

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();
}

服务器代码工作正常,我使用邮递员正常收到 POST 请求。问题是,当我使用 fetch 从客户端发送请求时...它以 POST 形式到达...非常奇怪。

我预计 POST 请求会到达服务端。

使用邮递员测试服务器可以找到。

对于我的客户端,这是服务器收到的消息:

/获取{ 主机:'本地主机:3001', 连接:“保持活动”, 'sec-ch-ua': '"Chromium";v="112", "Google Chrome";v="112", "Not:A-Brand";v="99"', 'sec-ch-ua-mobile': '?0', 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, 如 Gecko) Chrome/112.0.0.0 Safari/537.36',

'sec-ch-ua-platform': '"Windows"', 接受:'/', 来源:'http://localhost:3000', 'sec-fetch-site': '同一站点', 'sec-fetch-mode': 'cors', 'sec-fetch-dest': '空', 引用者:'http://localhost:3000/', '接受编码': 'gzip, deflate, br', '接受语言': 'en,en-US;q=0.9,es;q=0.8,pt-BR;q=0.7,pt;q=0.6' }

P粉316423089
P粉316423089

全部回复(1)
P粉116631591

尝试在 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'
  });
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!