Guest User

Untitled

a guest
Oct 21st, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. // 1. create a new XMLHttpRequest object -- an object like any other!
  2. var xhr = new XMLHttpRequest();
  3. // 2. open the request and pass the HTTP method name and the resource as parameters
  4. xhr.open('GET', 'file.html', true);
  5. // 3. write a function that runs once the Ajax request resolves
  6. xhr.onload = function () {
  7. // 4. check if the request is done and responded with success code
  8. if (xhr.readyState === xhr.DONE && xhr.status >= 200 && xhr.status < 300) {
  9. console.log(xhr.response);
  10. console.log(xhr.responseText);
  11.  
  12. // 5. insert the text sent by the server into an HTML element with id="ajax-content"
  13. document.getElementById('ajax-content').innerHTML = xhr.responseText;
  14. }
  15. };
  16.  
  17. var xhr = new XMLHttpRequest();
  18. xhr.responseType = 'document';
  19. xhr.open('GET', 'file.html', true);
  20. xhr.onload = function () {
  21. if (xhr.readyState === xhr.DONE && xhr.status >= 200 && xhr.status < 300) {
  22. console.log(xhr.response);
  23. console.log(xhr.responseXML);
  24.  
  25. // do stuff with xhr.responseXML...
  26. }
  27. };
Add Comment
Please, Sign In to add comment