• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

XmlHttpRequest object undefined js error

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>";
}
}
}
}

 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 608
Firefox Browser Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic