ivapopova

ajax_data_retrieve_problem.js

Jan 19th, 2019
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. // DOM cache
  2. var nodes={
  3. output: document.getElementsByClassName('output')[0],
  4. getDataBtn: document.getElementsByClassName('getDataBtn')[0],
  5. };
  6.  
  7. var AJAXcall = function(dataURL){
  8. // create a XMLHttpRequest object
  9. var xhr = window.XMLHttpRequest ? new XMLHttpRequest() :
  10. new ActiveXObject("Microsoft.XMLHTTP");
  11.  
  12. // initializes the request:
  13. xhr.open("GET", dataURL);
  14.  
  15. // sends the request:
  16. xhr.send();
  17.  
  18. // on ready (4)
  19. let res;
  20. xhr.onreadystatechange = function(){
  21. console.log(`@@@@@@@@@state: ${this.readyState}`);
  22. if( this.readyState == 4){
  23. if( this.status == 200){
  24. res = xhr.response;
  25. }
  26. }
  27. }
  28.  
  29. return res;
  30. };
  31.  
  32. function outputData(data){
  33. nodes.output.innerHTML = data;
  34. }
  35.  
  36.  
  37. nodes.getDataBtn.addEventListener('click', function(){
  38. let data = AJAXcall('/loremIpsum.txt');
  39. nodes.output.innerHTML = data;
  40. // Why not working!!!
  41. });
Advertisement
Add Comment
Please, Sign In to add comment