Get and display data from server using ajax -
i'm trying learn ajax i'm having difficult time.
i have java servlet give me data db, , i'm trying make simple web page continuously asks update every 5 seconds, , displays without having reload page. don't want on ready state change, once every 5 seconds.
// xmlhttp = xmlhttprequest object function process() { try { if (xmlhttp.readystate==4 || xmlhttp.readystate==0) { xmlhttp.open("get",'localhost:8080',true); //handleserverresponse(); // data server. not on ready state change, ? setinterval('process()', 5000); } else { settimeout('process()', 5000); } } catch (e) { alert('main process did not work'); alert(e.tostring()); } }
i unsure of how accomplish this. what's missing ?
the following steps involved getting data server.
- make ajax request object , open connection
- and send request server
- the server continuously sends response while processing request. ex. 200 send when request successful , 404 when cannot find resource. ready state tells how state of request there 5 states informs state of our request server . look here
only when readystate 4 , status 200. request server successful , response in
request.responsehtml
orrequest.responsexml
. use processing.windown.onload = initialize; function initialize() { setinterval(process, 5000); } var request ; function process() { request = getajaxrequetobject; request.open("get","url",true); request.onreadystatechange = requestresponse; request.send(true); } function requestresponse() { if(request.readystate == 4 && request.status == 200) { // manipulation request.responsehtml or request.responsexml } }
Comments
Post a Comment