Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Ajax
|
Ajax the combination of XHTML, CSS, DOM, XML, XSLT, XMLHttpRequest, and JavaScript
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Ajax section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 20th, 2006, 02:51 AM
Registered User
 
Join Date: Mar 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Why is readyState always 1?

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.subscriptio nID.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]

 
Old March 20th, 2006, 04:42 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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)
 
Old March 20th, 2006, 12:01 PM
Registered User
 
Join Date: Mar 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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/developerwork...wa-ajax-i.html

WAR FILE:
---------
ftp://www6.software.ibm.com/software...ax-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

 
Old March 27th, 2006, 10:37 AM
jmcpeak's Avatar
Wrox Author
 
Join Date: Nov 2005
Posts: 87
Thanks: 0
Thanked 18 Times in 17 Posts
Default

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:

Code:
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:

Code:
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
 
Old January 3rd, 2007, 01:54 PM
Authorized User
 
Join Date: May 2006
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to kaos_frack
Default

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.

 
Old April 29th, 2008, 07:35 PM
Registered User
 
Join Date: Apr 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
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/developerwork...wa-ajax-i.html

WAR FILE:
---------
ftp://www6.software.ibm.com/software...ax-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 ****************ing 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.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Always getting readyState = 1 CraigMuckleston PHP How-To 0 June 15th, 2007 10:55 AM
readyState won't move from 1 to 2! lmireskandari Javascript 0 February 28th, 2007 11:50 AM
readyState not ready -- closed see google groups scottradny BOOK: Professional Ajax ISBN: 978-0-471-77778-6 1 March 30th, 2006 11:55 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.