Advertisement
karlakmkj

Reconstruct XHR POST request

Dec 30th, 2020
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //boilerplate code for an AJAX POST request using an XMLHttpRequest object.
  2. const xhr = new XMLHttpRequest();
  3.  
  4. const url = 'https://api-to-call.com/endpoint';
  5. const data = JSON.stringify({id: '200'});  //JSON.stringify() will convert a value to a JSON string. By converting the value to a string, we can then send the data to a server.
  6.  
  7. xhr.responseType = 'json';
  8.  
  9. xhr.onreadystatechange = () => {
  10.   if (xhr.readyState === XMLHttpRequest.DONE){
  11.     return xhr.response;
  12.   }
  13. }
  14.  
  15. xhr.open('POST', url);
  16. xhr.send(data);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement