0

For a school project, I am to send JSON. The server processes this file and sends me back a response with a JSON. The whole thing should happen with JavaScript. I have now become aware of the XMLHttpRequest, but don't quite understand it.

Do I have to create two objects with GET and POST or is it possible to combine them?

At the moment I have one var httpPost = new XMLHttpRequest(); to send and one to receive var httpGet = new XMLHttpRequest();

evolutionxbox
  • 3,932
  • 6
  • 34
  • 51

1 Answers1

0

let's do it with jQuery AJAX api:

let json = {"key":"val"}
$.post("api.php",json, function(response){
    console.log(response)
});

let's do it with Fetch api:

const data = { username: 'example' };
fetch('api.php', { method: 'POST', body: JSON.stringify(data) })
.then(response => {
  console.log('Success:', response);
}) 
ANOL GHOSH
  • 1
  • 1
  • 9