why request readyState always 1?
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Jason_Chang
Posted On:   Thursday, January 25, 2007 10:52 PM

AJAX Demo } else { document.getElementById("output").innerHTML = "server down!!!"; } } else { document.getElementById("output").innerHTML = "loading..." + rs; } } ]]> The client.html is as follows: AJAX Sample for loop The server.jsp is very simple: <% String[] str = new String[] {"Love", "Power", "Peace", "Sun", "Moon", "Star", "ET"}; int number = new java.util.Random().nextInt(6+1); response.getWriter().write(str[number])   More>>
AJAX Demo }
else {
document.getElementById("output").innerHTML = "server down!!!";
}
} else {
document.getElementById("output").innerHTML = "loading..." + rs;
}
}
]]>


The client.html is as follows:


			







AJAX Sample for loop








The server.jsp is very simple:



			
<%
String[] str = new String[] {"Love", "Power", "Peace", "Sun", "Moon", "Star", "ET"};
int number = new java.util.Random().nextInt(6+1);
response.getWriter().write(str[number]);
%>



It works fine when server recovers from down (not only stop service, also shut down server); but when server is down, remote client-side request readyState always 1 and never goes to 4(completed).




Can anyone help? Appreciate in advance.

   <<Less

Re: why request readyState always 1?

Posted By:   Axel_Richter  
Posted On:   Saturday, February 3, 2007 11:10 AM

Hi,



*argh* ;-) I read its post not completely:



>>It works fine when testing locally, i.e., client running at localhost. However, when testing client at remote machine, the request readyState always be 1




Try the following:


function getGiftFromServer() {
var url = "http://192.168.0.100:8080/ajaxtest/server.jsp";
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}

xmlHttp.onreadystatechange = showGift;

try {
xmlHttp.open("GET", url, true);
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlHttp.send("null");
} catch (ex) {
alert(ex);
}
}

Maybe there is a security violation because of the same origin policy. The statement alert(ex); will show this.


Greetings





Axel

Re: why request readyState always 1?

Posted By:   Axel_Richter  
Posted On:   Friday, January 26, 2007 08:23 AM

Hi,



your function getGiftFromServer() is called every 2 seconds. This function sends an asynchronous HTTP-Get-Request to url. On redystate change this asynchronous HTTP-Get-Request is calling the function showGift(). The first redystate change ist from 0 to 1. What if it takes more than 2 seconds to complete one request? Then the next redystate change from the next request is calling the function showGift() and so on.



Why do'nt you call setTimeout("getGiftFromServer()", 2000); if one request is ready?



Greetings



Axel
About | Sitemap | Contact