AJAX( JavaServlet, JavaScript, XMLHTTPRequest )
I'm trying to build a JavaServlet to serve XML to a Client-Side JavaScript.
I'm using -
Sun Java System Application Server (Bundled with NetBeans 5.5.1)
Firefox 2.0.0.3
What I'm doing is feasible for the browser. This is exactly how www.microsoft.com works.
Here's the background:
I'm calling the server via an XMLHTTPRequest object in JavaScript. Basically, the client side JavaScript looks like:
var client =new XMLHttpRequest();
client.open('POST', myurl,false);
client.setRequestHeader('Content-Type',
'application/x-www-form-urlencoded; charset=utf-8');
client.send("arg1=data1&arg2=data2");
alert("This is what the server says:" + client.responseText);
if (client.responseXML !=null)
alert("I should be seeing this message.");
else
alert("but I'm seeing this message instead.");
on the server side...
class myServerextends HttpServlet{
publicvoid
doPost(
HttpServletRequest request,
HttpServletResponse response
)
throws ServletException, IOException
{
doGet(request, response);
}
publicvoid
doGet(
HttpServletRequest request,
HttpServletResponse response
)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
out.println("<string>somestring</string>");
out.close();
}
}
Here's the problem:
The XMLHTTPRequest object is getting the response from the server, but it is not populating 'responseXML'; it is null. responseText is being populated with the output from the server, however.
Some other clues may be:
I have tried the pattern where one specifies a handler() method for the XMLHTTPRequest's 'onreadystatechange' member. When I do this, I can observe the handler() method being called several times, presumably as the XMLHTTPRequest object changes state, however, apparently all of its members remain null, including 'this.readyState', 'this.status', 'this.responseText' and 'this.responseXML' as called from the handler() method, throughout the entirety of the object's operation.
If I use a 'GET' instead of a post, no data returns, not even 'responseText'.
This seems particularly strange, as, in the server, the doPost method is just a wrapper for the doGet method.
finally, the following javascript function, lifted directly from active duty on microsoft's website, demonstrates the same problem when served from my server
function doRequest(url, urlParmString){
var result =null;
var callBack =new XMLHttpRequest();
if(callBack){
try{
callBack.open('POST',url,false);
if(urlParmString==null)urlParmString='';
callBack.setRequestHeader(
'Content-Type',
'application/x-www-form-urlencoded; charset=utf-8');
callBack.send(urlParmString);
if(callBack.responseXML.childNodes[0] && callBack.responseXML.childNodes[0].firstChild){
result = callBack.responseText.substring(callBack.responseText.indexOf("%"));
result = result.replace("</string>","");
result = unescape(result);}
elseif(callBack.responseXML.childNodes.length > 1){
result = unescape(callBack.responseXML.childNodes[1].text);
}
}
catch(e){;
if(debug){
if(callBack.responseText !=null && callBack.responseText !='')
alert('DoRequest() Error:\n' + e +"\n\n" + callBack.responseText);
else alert('DoRequest() Error:\n' + e);
}
subNavClose();
}
callBack =null;
}
else{
if(debug)
alert('Dev Debug error: callBack is false');
}
return result;
}

