javascript - simple ajax nothing happens -
i trying create small ajax script add text div. nothing happen, it's killing me.
please help.
html:
<!doctype> <html> <head> <script type="text/javascript" src="ajax.js"></script> <script type="text/javascript" src="jquery.js"></script> </head> <body onload="process()"> ok, made far <br/> <div id="thed"> </div> </body> </html>
ajax.js:
var xmlhttp= createxmlhttprequestobject(); function createxmlhttprequestobject(){ var xmlhttp; if (window.xmlhttprequest)( xmlhttp = new xmlhttprequest(); )else{ xmlhttp = new activexobject("microsoft.xmlhttp") } return xmlhttp; } function process(){ alert('hi'); if (xmlhttp){ try{ xmlhttp.open("get", "ajax.txt", true); xmlhttp.onreadystatechange = handleserverresponse; xmlhttp.send(null); }catch(e){ alert(e.tostring()); } } } function handleserverresponse(){ thed = documet.getelementbyid('thed'); if (xmlhttp.readystate==1){ thed.innerhtml += "status1:server connection established <br/>"; }else if (xmlhttp.readystate==4){ if (xmlhttp.status=200){ try{ text=xmlhttp.responsetext thed.innerhtml += "status4:request finish<br/>"; thed.innerhtml += text; }catch(e){ alert(e.tostring); } }else{ alert((xmlhttp.statustext); } } }
the ajax.txt contain simple string.
this xhr2 if want more browser support can extend it.
<!doctype html> <html> <head> <meta charset="utf-8"> <title>ajax</title> <script> function ajax(a,b,c){ // url, callback, placeholder c=new xmlhttprequest; c.open('get',a); c.onload=b; c.send() } function h(){ document.getelementbyid('thed').innertext=this.response } window.onload=function(){ ajax('ajax.txt',h); } </script> </head> <body> <div id="thed"></div> </body> </html>
if have questions how works or how can extend ask
here have more info
https://stackoverflow.com/a/18309057/2450730
you can add ie support
by replacing
c=new xmlhttprequest;
with
c=new xmlhttprequest||new activexobject("msxml2.xmlhttp.3.0");
and using onreadystatechange
Comments
Post a Comment