|
Subject:
|
Why is readyState always 1?
|
|
Posted By:
|
sqad
|
Post Date:
|
3/20/2006 1:51:55 AM
|
Dev-folks,
I have the following *.js file. Can anyone explain to me why my readyState is always 1 in the validate(...) fxn? Shouldn't it be 2? I have a WebApp running on Glassfish. I can provide more details upon request. Thanks.
[snippet] // creating the XMLHttpRequest obj on Microsoft browsers var xmlHttp = false;
// set initial focus on subscription id field on page load function startup() { document.forms[0].subscriptionID.focus(); }
function validate (formObj) { init(); xmlHttp.onreadystatechange = subscriptionValidator; xmlHttp.send("subscriptionID="+formObj.subscriptionID.value); alert("readyState = " + xmlHttp.readyState); }
function init() { /*@cc_on @*/ /*@if (@_jscript_version >= 5) try { xmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e2) { xmlHttp = false; } } @end @*/ if( !xmlHttp && typeof XMLHttpRequest != 'undefined') { xmlHttp = new XMLHttpRequest(); } var url = "/BookServlet"; xmlHttp.open("POST", url, true); xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); }
[/snippet]
|
|
Reply By:
|
joefawcett
|
Reply Date:
|
3/20/2006 3:42:44 AM
|
I think you're right, it should be 1 (LOADED) but I've found that other than the COMPLETED (4) one they are all unreliable. That's the trouble with asynchronous requests...
--
Joe (Microsoft MVP - XML)
|
|
Reply By:
|
sqad
|
Reply Date:
|
3/20/2006 11:01:09 AM
|
Hi Joe or anyone for that matter,
Can you run this sample application (WAR file) from IBM's tutorial?
IBM'S LINK: ----------- http://www-128.ibm.com/developerworks/edu/wa-dw-wa-ajax-i.html
WAR FILE: --------- ftp://www6.software.ibm.com/software/developer/education/wa-ajax-Library.war
I deployed the war file in Glassfish, but I can't get the application to work. After I enter the name and press tab, nothing happens. Can anyone try it and let me know, if they get it working? The code looks correct to me. Perhaps, I am doing something wrong :-(
Thanks!
/sqad
|
|
Reply By:
|
jmcpeak
|
Reply Date:
|
3/27/2006 9:37:59 AM
|
You're always getting a readyState of 1 for a couple of reasons.
First, you're making an asynchronous call and immediately checking the readyState afterwards:
xmlHttp.send("subscriptionID="+formObj.subscriptionID.value);
alert("readyState = " + xmlHttp.readyState); You will always get a readyState of 1 in this situation. Second, you should be checking the readyState inside the onreadystatechange event handler. You've assigned subscriptionValidator to handle the event, so do your readyState and Http status checks there.
Just to give you an idea of what an onreadystatechange event handler should look like:
xmlHttp.onreadystatechange = function () {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200 || xmlHttp.status == 304) {
processData(xmlHttp.responseText);
}
}
}; Nicholas covers XmlHttp requests on page 35 in Pro. Ajax. If you have the book, you might want to check that section out.
------------------------ Jeremy McPeak Author, Professional Ajax http://www.wdonline.com
|
|
Reply By:
|
kaos_frack
|
Reply Date:
|
1/3/2007 12:54:12 PM
|
This is not about your problem, but I see from reading posts that people are using different methods to create XmlHttp object by trying to identify what version/type of this object is available. I really recommend using the cross-browser XmlHttp object by Nicholas available on his website: http://www.nczonline.net/ I'm using it for my project. It might solve many of your current and future problems.
|
|
Reply By:
|
onePost
|
Reply Date:
|
4/29/2008 7:35:37 PM
|
quote: Originally posted by sqad
Hi Joe or anyone for that matter,
Can you run this sample application (WAR file) from IBM's tutorial?
IBM'S LINK: ----------- http://www-128.ibm.com/developerworks/edu/wa-dw-wa-ajax-i.html
WAR FILE: --------- ftp://www6.software.ibm.com/software/developer/education/wa-ajax-Library.war
I deployed the war file in Glassfish, but I can't get the application to work. After I enter the name and press tab, nothing happens. Can anyone try it and let me know, if they get it working? The code looks correct to me. Perhaps, I am doing something wrong :-(
Thanks!
/sqad
hopefully, one day someone will read this post and this will help them. the problem here is that your teacher is a fucking moron and didn't check to see if the homework he assigned you actually worked.
first, make sure that there are no javascript errors on your page. using firefox, you will know that things arent working if there are a lot of "XXX function is not defined" errors in the error console when you visit this page. this means that your relative paths arent working properly based on the way the war file deployed.
once/if you arent getting javascript errors, go into the "library.js" file and look for any instance of the "readystate" method being invoked. THIS IS WRONG. "readyState" is case sensitive. because its all lowercase, this returns "undefined" and therefore, "undefined" never equals "4", and the final part is never run. making the S in "state" capital will return proper values, and the code will work.
i had to figure this out on my own. my teacher is an idiot.
|