aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes XmlHttpRequest object undefined js error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "XmlHttpRequest object undefined js error" Watch "XmlHttpRequest object undefined js error" New topic
Author

XmlHttpRequest object undefined js error

Bushra Binte
Ranch Hand

Joined: Nov 07, 2006
Posts: 60
Hi,I am trying to implement ajax for authenticating the login credentials. onkeyup() event ,i invoke a js function validate() as below:

function validate() {
alert("hey here..");
var idField = document.getElementById("userName");
alert(idField.value);
var pwdField = document.getElementById("password");
alert(pwdField.value);
var url = "/Login.do?userName=" + escape(idField.value)+"pwd=" + escape(pwdField.value);

if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
alert("xmlhttp: "+req.value);
} else if (window.ActiveXObject) {
alert("xmlhttp: "+req);
req = new ActiveXObject("Microsoft.XMLHTTP");
}

req.open("GET", url, true);
req.onreadystatechange = callback;
req.send(null);
}


But i get a js error tha says XMLHttpRequest object is undefined.I am using IE6.0 and the webserver is tomcat6.0.18.
I set the below headers in the servlet called:

response.setContentType("text/xml");
response.setHeader("Cache-Control", "no-cache");
response.getWriter().write(“exists");


When i get the response from the server,callback() function is called.

function callback() {

alert("from callback");
if (req.readyState == 4) {
if (req.status == 200) {
// parse the response and get the message
message = req.getResponseText;
mdiv = document.getElementById("userIdMessage");
if (message == "exists")
{
mdiv.innerHTML = "<div style=\"color:red\">User Id exists</ div>";
} else {
mdiv.innerHTML = "<div style=\"color:green\">Valid User Id</ div>";
}
}
}
}

Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
In the future, please use code tags when posting your code. Makes it easier to read

also you alert is before you declare the req object for ActiveX.

Eric
Bushra Binte
Ranch Hand

Joined: Nov 07, 2006
Posts: 60
Thanks Eric,will take care in the future posts.
I have put the alert after the XmlHttpRequest object creation statement in the js,but still get an error 'undefined'.

Could any one kindly suggest
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
You really should use a library. Try doing something like this:




Eric
Bushra Binte
Ranch Hand

Joined: Nov 07, 2006
Posts: 60

My requirement is to authenicate the user 2) auto populate the text box with similiar names.For eg, when the user types in 'r',i need to fetch all the names starting with 'r' like 'rita','rosy','roseline' etc and show as a dropdown list.



Also could you please suggest library to use.
Saifuddin Merchant
Ranch Hand

Joined: Feb 08, 2009
Posts: 605

If Ajax is your main aim two really useful libraries would be Prototype & JQuery
Prototype is a good library to begin with - its pretty easier to use.

My personal suggestion would be to go for Jquery. Its a bit harder and I think it definitely needs to be learned (recommended book Jquery in Action - I really loved it) but it well worth the time spend to learn it.

Of course if all you need is do a little Ajax then stick to prototype.


Cheers - Sam.
Twisters - The new age Java Quiz || My Blog
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: XmlHttpRequest object undefined js error