I’m working through the example and have converted the php server code to ASP.NET/C#. I’m getting a Javascript error though and believe it has something to do with how I’m returning the XML to the Response stream. Can anyone help me diagnose this thing?
default.aspx (form with js code)
<%@ Page language="c#" Codebehind="default.aspx.cs" AutoEventWireup="false" Inherits="XmlHttpRequest._default" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
<title>XmlHttpRequest Example</title>
<style type="text/css">
span.hidden
{
display: none;
}
span.error
{
display: inline;
color: black;
background-color: pink;
}
</style>
<script type="text/javascript">
var req;
function loadXMLDoc(url)
{
if (window.XMLHttpRequest)
{
// branch for native XMLHttpRequest object
req = new XMLHttpRequest();
req.onreadystatechange = processReqChange;
req.open("GET", url, true);
req.send(null);
}
else if (window.ActiveXObject)
{
// branch for IE/Windows ActiveX version
req = new ActiveXObject("Microsoft.XMLHTTP");
if (req)
{
req.onreadystatechange = processReqChange;
req.open("GET", url, true);
req.send();
}
}
}
function processReqChange()
{
// only if req shows "complete"
if (req.readyState == 4)
{
// only if "OK"
if (req.status == 200)
{
response = req.responseXML.documentElement;
method = response.getElementsByTagName('method')[0].firstChild.data;
result = response.getElementsByTagName('result')[0].firstChild.data;
eval(method + '(\\'\\', result)');
}
else
{
alert("There was a problem retrieving the XML data:\
" + req.statusText);
}
}
}
function checkName(input, response)
{
if (response != '')
{
// Response mode
message = document.getElementById('nameCheckFailed');
if (response == 'true')
message.className = 'error';
else
message.className = 'hidden';
}
else
{
// Input mode
url = 'http://localhost/XmlHttpRequest/checkUserName.aspx?q=' + input;
loadXMLDoc(url);
}
}
</script>
</head>
<body>
<input id="username" name="username" type="text" onblur="checkName(this.value,'')" />
<span class="hidden" id="nameCheckFailed">
This name is in use, please try another.
</span>
</body>
</html>
My error is coming from this js function:
It keeps saying the response object is null on the bolded line.
function processReqChange()
{
// only if req shows "complete"
if (req.readyState == 4)
{
// only if "OK"
if (req.status == 200)
{
response = req.responseXML.documentElement;
[i][u][b]method = response.getElementsByTagName('method')[0].firstChild.data;[/b][/u][/i]
result = response.getElementsByTagName('result')[0].firstChild.data;
eval(method + '(\\'\\', result)');
}
else
{
alert("There was a problem retrieving the XML data:\
" + req.statusText);
}
}
}
Just as an FYI, I just went through same problem, I had a struts action which was writing to the PrintWriter and all was well, I then switched over to a velocity template (to do the right thing) and was getting the null response object.
Turns out that my velocity template just needed an extra newline at the end. Seems that the req.responseXML.documentElement needs a newline at very end. What was maddening was that the req.responseText looked fine!