Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

XmlHttpRequest object undefined js error

 
Bushra Binte
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You really should use a library. Try doing something like this:




Eric
 
Bushra Binte
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 607
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic