• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

AJAX: request not sent properly

 
Madhura Phansalkar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI
I am trying out a simple ajax example
my problem is, when
req.send(null) is called, the servlet handling this request is not called..

i have added the code


var idField = document.getElementById("in");
var url = "validate?id=" + escape(idField.value);

if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
} else if (window.ActiveXObject) {
req = new ActiveXObject("Microsoft.XMLHTTP");
}

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


i have also written the calback function in javascript..

web.xml has following entry

<servlet>
<servlet-name>ValidateServlet</servlet-name>
<servlet-class>ValidateServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ValidateServlet</servlet-name>
<url-pattern>/validate</url-pattern>
</servlet-mapping>


can you please help me....


thanks in advance

Madhura P
 
Kiran Joshi
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May be you can verify if you have overriden doGet() method in the servlet.
Try running your servlet alone directly from the browser typing the url that you are preparing in the address bar.
 
Madhura Phansalkar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Thanks.
the servlets runs independently and is giving XML document back in response.
i am not understanding, why request.send() not invoking invoking this request
 
Madhura Phansalkar
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can this be a browser issue? i tried on MS-IE and mozilla as well.. doent work anways
 
Kiran Joshi
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try modifying your code like below.
<code>
..........
else if (window.ActiveXObject) { // IE
try {
http_request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
http_request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {}
}
}

if (!http_request) {
alert('Giving up Cannot create an XMLHTTP instance');
return false;
}
</code>
 
Kiran Joshi
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried executing your code without above changes suggested by me & it works.It's calling my servlet & getting the results as well.
I tested it on IE version 6.0.2800.1106 (IE6)
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are your sure your JavaScript is being called?

Eric
 
Kiran Joshi
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
below code is working fine.

<html>

<head>

<script language='javascript'>
function firerequest()
{
alert('calling firerequest');
var idField = document.getElementById("in");
var url = "Ajax?sDeptCode=" + escape(idField.value);

if (window.XMLHttpRequest) {
req = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
req = new ActiveXObject("Microsoft.XMLHTTP");
}
if(!req)
{
alert('unable to create request object');
}
alert(url);

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


alert('request sent');
}

function callback()
{if (req.readyState == 4) {
if (req.status == 200) {
alert(req.responseText)
} else {
alert(req.responseText+http_request.status);
}
}
}
</script>
</head>

<body>
Input <input type = 'text' name='input' id='in'>

<input type='button' onklick='firerequest()'>
</body>

</html>
 
Kiran Joshi
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read onklick as onClick.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic