javascript - Ajax request to load .php file not working -


trying load contents postcode.php file #postcodelist div, not working (nothing happens). checked postcode.php file echoes al correct information.

var loadicecream = document.getelementbyid("icecreams"); loadicecream.addeventlistener("click", iceajax, false);  function iceajax() {     var xmlhttp;     if (window.xmlhttprequest) {       xmlhttp = new xmlhttprequest();     }     else {       xmlhttp = new activexobject("microsoft.xmlhttp");     }      xmlhttp.open("post","ice-cream-list.php",true);     xmlhttp.send();      document.getelementbyid("ice-cream-list").innerhtml = xmlhttp.responsetext; } 

you want query execute asynchronously (the third parameter open function) , synchronously try read value. happens before query has been sent, empty.

either run load synchronously, or set xmlhttp.onreadystatechange point function handle loaded state. best way asynchronously since don't want block user using page while loading data.

quick example, handles success case:

var xmlhttp; if (window.xmlhttprequest) {   xmlhttp = new xmlhttprequest(); } else {   xmlhttp = new activexobject("microsoft.xmlhttp"); }  xmlhttp.onreadystatechange  = function() {     if (xmlhttp.readystate == 4 && xmlhttp.status == 200)     {         document.getelementbyid("postcodelist").innerhtml = xmlhttp.responsetext;     } }  xmlhttp.open("post","postcode.php",true); xmlhttp.send(); 

read on documentation onreadystatechange, @ least want handle case there timeout or error, otherwise user won't know went wrong.


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -