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.

  1. make ajax request object , open connection
  2. and send request server
  3. 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
  4. only when readystate 4 , status 200. request server successful , response in request.responsehtml or request.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

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

[C++][SFML 2.2] Strange Performance Issues - Moving Mouse Lowers CPU Usage -

ios - Possible to get UIButton sizeThatFits to work? -