It's not a secret anymore!*
The moose likes HTML, CSS and JavaScript and the fly likes AJAX: request not sent properly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "AJAX: request not sent properly" Watch "AJAX: request not sent properly" New topic
Author

AJAX: request not sent properly

Madhura Phansalkar
Greenhorn

Joined: Jun 12, 2006
Posts: 9
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

Joined: Sep 04, 2005
Posts: 54
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

Joined: Jun 12, 2006
Posts: 9
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

Joined: Jun 12, 2006
Posts: 9
can this be a browser issue? i tried on MS-IE and mozilla as well.. doent work anways
Kiran Joshi
Ranch Hand

Joined: Sep 04, 2005
Posts: 54
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

Joined: Sep 04, 2005
Posts: 54
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

Joined: Nov 08, 2001
Posts: 15376
    
    6
Are your sure your JavaScript is being called?

Eric
Kiran Joshi
Ranch Hand

Joined: Sep 04, 2005
Posts: 54
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

Joined: Sep 04, 2005
Posts: 54
Please read onklick as onClick.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: AJAX: request not sent properly