Posted By

daisyfig99 on 03/13/13


Tagged


Versions (?)

Ajax Request & Handler using just Javascript & JSfiddle


 / Published in: JavaScript
 

I prefer to use jQuery but hey if you want to do it in JavaScript, then here you go. Maybe I will update this code soon and have a working example in JSfiddle.

  1. var aRequest;
  2.  
  3. //Makes an AJAX request & checks which request current broswer supports.
  4. if (window.XMLHttpRequest){
  5. aRequest = new XMLHttpRequest();
  6. }
  7. else if (window.ActiveXObject){
  8. aRequest = new ActiveXObject("Microsoft.XMLHTTP");
  9. }
  10.  
  11. //Deals with the request
  12. aRequest.onreadystatechange = function(){
  13. //if request has completely finished.
  14. if (aRequest.readyState === 4 && xmlhttp.status===200){
  15. //code for whatever you want to do with the data goes here.
  16. var requestedData = innerHTML=xmlhttp.responseText;
  17. console.log("Data:" + requestedData);
  18. }
  19.  
  20. };
  21.  
  22. //change this to desired path. it can be xml, html, etc. this is for testing ajax in JSfiddle.
  23. aRequest.open("POST", "/echo/html/", true);
  24.  
  25. //no, we do not need to send data back.
  26. aRequest.send();

Report this snippet  

You need to login to post a comment.