0
<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>
2
Contributors
6
Replies
7
Views
8 Years
Discussion Span
Last Post by lkofman
0

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);
0

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

0

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.

0

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.

0

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?

0

yes requesting dqirectly from address bar servlet returns the answer

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.