0

I send data from one server to another with XMLhttpRequest but this code doesn't work:

  var AjaxURL = 'http://localhost:5000/index.html';
  $.ajax({
      type: "GET",
      url: AjaxURL,
      data: { 
        foo: "test01"
      },
      success: function(result) {
          window.console.log('Successful');
      }
  });

(Console log: "Successful") I have tried it with an iframe and it works but I need to send it with ajax or XMLhttpRequest.

const socket = io();

let params = new URLSearchParams(location.search);
let a1 = params.get('foo');

socket.emit('data', a1);
socket.on('data', (data) => {
        const fileName = './src/data.json';
        const file = require(fileName);
        file.data = data;
        
        fs.writeFile(fileName, JSON.stringify(file), function writeJSON(err) {
        if (err) return console.log(err);
        console.log('writing to ' + fileName + ':' + data);
        });
    })
{"data":"testing"}

Thanks

  • I'd highly suggest you use a database like [MySQL](https://www.mysql.com/) or [mongodb](https://www.mongodb.com/). To store data inside a JSON file via FS is most certainly not the way forward especially not for large services. – Tyler2P Aug 28 '21 at 15:48
  • I am using a JSON file only for testing the code – Club Asirio Huelva Aug 28 '21 at 15:49
  • Can we see what your `data.json` file looks like? – Tyler2P Aug 28 '21 at 15:50
  • I have updated the question with the JSON file, but I have tried to enter data directly by accessing the URL (http://localhost:5000/index.html?foo=testing) and it works, the problem must be in the sending of the data – Club Asirio Huelva Aug 28 '21 at 15:55
  • Are you aware that there's a comma in the URL instead of a dot? (first code snippet). It should be `index.html` not `index,html` – Tyler2P Aug 28 '21 at 15:59
  • @ClubAsirioHuelva Please check in developer console is there any warning regarding "CORS" or something ; if so follow [this](https://stackoverflow.com/questions/25845203/understanding-cors) – Vinay Aug 28 '21 at 16:09
  • I have updated the question, I have changed the code, but it still does not work, in the console log it only shows "successful" @Vinay – Club Asirio Huelva Aug 28 '21 at 16:14

0 Answers0