來自react.js的POST以GET形式到達node/js伺服器
P粉316423089
P粉316423089 2024-04-03 17:22:04
0
1
337

看來我的 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,0; Win64; x64) AppleWebKit/537.36 (KHTML, 如 Gecko) Chrome/112.0.0.0 Safari/537.36',

#######################################################################1/ '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學習者快速成長!