Rumah  >  Soal Jawab  >  teks badan

POST daripada react.js mencapai pelayan nod/js sebagai GET

Nampaknya permintaan POST saya sedang ditukar kepada GET di suatu tempat dalam kod pelanggan REACT saya. Ini adalah kod tindak balas:

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

Ini adalah kod pelayan:

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

Kod pelayan berfungsi dengan baik dan saya menerima permintaan POST biasanya menggunakan Posmen. Masalahnya ialah apabila saya menghantar permintaan daripada pelanggan menggunakan fetch ... ia tiba sebagai POST ... sangat pelik.

Saya menjangkakan permintaan POST akan sampai ke pelayan.

Boleh didapati menggunakan pelayan ujian Posmen.

Untuk pelanggan saya, ini adalah mesej yang diterima oleh pelayan:

/dapat{ hos: 'localhost:3001', sambungan: "terus hidup", '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, seperti Gecko) Chrome/112.0.0.0 Safari/537.36',

'sec-ch-ua-platform': '"Windows"', Terima: '/', Sumber: 'http://localhost:3000', 'sec-fetch-site': 'Tapak yang sama', 'sec-fetch-mode': 'cors', 'sec-fetch-dest': 'kosong', Dipetik oleh: 'http://localhost:3000/', 'Terima pengekodan': 'gzip, kempis, br', 'Bahasa penerimaan': 'en,en-US;q=0.9,es;q=0.8,pt-BR;q=0.7,pt;q=0.6' }

P粉316423089P粉316423089170 hari yang lalu325

membalas semua(1)saya akan balas

  • P粉116631591

    P粉1166315912024-04-04 00:22:48

    Cuba tetapkan semuanya kepada huruf kecil dalam kaedah fetch; ini sepatutnya berjaya kerana ia sensitif huruf besar.

    Seperti ini:

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

    balas
    0
  • Batalbalas