<html>
   <head>
    <title>Ajax demo</title>

   </head>
   <body >
   <script type= "text/javascript" >
   var req;

     function   validateUser() {

      alert("validate catched")
    if(window.XMLHttpRequest){
       req=new XMLHttpRequest();
    } else if (window.ActiveXObject) {
    req=new ActiveXObject("Microsoft.XMLHTTP");
    }
    else{
    alert("Your browser does not support XMLHTTP!");
    }
    alert("request catched")
       var  idField=document.getElementById("userid");
       var url="localhost:1234/a/validate?id=" ;
       url+=idField.value;
       req.open("GET",url,true);
       req.send(null);
   }
     </script>
     <H1>Ajax Demo1</H1>

    <form>
        <input type="text"  size="20"  id="userid" name="id"  onclick="validateUser();">
    </form>
   <div id="userIdMessage" size="20">
   </div>
   </body>
</html>

Recommended Answers

All 6 Replies

You are missing the onreadystatechange property, you will need this to receive the data returned from the server
It should look similar to this.

url+=idField.value;
req.open("GET",url,true);
req.onreadystatechange = function() {
        if (req.readyState == 4) {
            document.getElementById('userIdMessage').innerHTML = req.responseText;
        }
}
req.send(null);

In FF 3.5 readyState i always receive 1,in IE it work.
Do you have any idea?

Can u post the code u are using, i edited the one here to this and it works in FF:

function   validateUser() {

    if(window.XMLHttpRequest){
       req=new XMLHttpRequest();
    } else if (window.ActiveXObject) {
    req=new ActiveXObject("Microsoft.XMLHTTP");
    }
    else{
    alert("Your browser does not support XMLHTTP!");
    }
       //var  idField=document.getElementById("userid");
       var url="serviceinput.jsp" ;
       url+="?userid=5";
	   req.open("GET",url,true);
	   req.onreadystatechange = function() {
	           if (req.readyState == 4) {
document.getElementById('userIdMessage').innerHTML = req.responseText;
	           }
	   }
req.send(null);
   }

I had to change the url, and comment some of the existing code, since i dont have the fields.

I request form servlet validate not from jsp

Can u post the code u are using, i edited the one here to this and it works in FF:

function   validateUser() {

    if(window.XMLHttpRequest){
       req=new XMLHttpRequest();
    } else if (window.ActiveXObject) {
    req=new ActiveXObject("Microsoft.XMLHTTP");
    }
    else{
    alert("Your browser does not support XMLHTTP!");
    }
       //var  idField=document.getElementById("userid");
       var url="serviceinput.jsp" ;
       url+="?userid=5";
	   req.open("GET",url,true);
	   req.onreadystatechange = function() {
	           if (req.readyState == 4) {
document.getElementById('userIdMessage').innerHTML = req.responseText;
	           }
	   }
req.send(null);
   }

I had to change the url, and comment some of the existing code, since i dont have the fields.

requesting from a servlet should still work, i just used a jsp, is the code the same though?

another thing, when u browse directly to the servlet using the url you are using in the ajax call, does the servelt return a result?

yes requesting dqirectly from address bar servlet returns the answer

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.