Advertisement
Guest User

Untitled

a guest
Jul 13th, 2018
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. var url = "MY-URL";
  2. var method = "POST";
  3. var postData = "Some data";
  4.  
  5. // You REALLY want shouldBeAsync = true.
  6. // Otherwise, it'll block ALL execution waiting for server response.
  7. var shouldBeAsync = true;
  8. var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
  9. var request = new XMLHttpRequest();
  10.  
  11. // Before we send anything, we first have to say what we will do when the
  12. // server responds. This seems backwards (say how we'll respond before we send
  13. // the request? huh?), but that's how Javascript works.
  14. // This function attached to the XMLHttpRequest "onload" property specifies how
  15. // the HTTP response will be handled.
  16. request.onload = function () {
  17.  
  18. // Because of javascript's fabulous closure concept, the XMLHttpRequest "request"
  19. // object declared above is available in this function even though this function
  20. // executes long after the request is sent and long after this function is
  21. // instantiated. This fact is CRUCIAL to the workings of XHR in ordinary
  22. // applications.
  23.  
  24. // You can get all kinds of information about the HTTP response.
  25. var status = request.status; // HTTP response status, e.g., 200 for "200 OK"
  26. var data = request.responseText; // Returned data, e.g., an HTML document.
  27. }
  28.  
  29. request.open(method, url, shouldBeAsync);
  30.  
  31. request.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
  32. // Or... request.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");
  33. // Or... whatever
  34.  
  35. // Actually sends the request to the server.
  36. request.send(postData)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement