Page 1 of 1

ajax problem parsing responseXML

Posted: Wed Aug 04, 2010 7:57 am
by shawngoldw
Hello, I am having a small problem parsing my response XML, here is the code I'm using and then I'll explain the problem.

Code: Select all

window.onload = function()
{
	document.getElementById("email").addEventListener("keyup", verify_email, false);
}

function verify_email()
{
	$params = "email=" + document.getElementById("email").value;
	send_request("handle_email", "email", $params);
}

function handle_email(ajax)
{
	var status = ajax.getElementsByTagName("status")[0].nodeValue;
}

function send_request(handler, processor, params, method)
{
	if (!handler || !processor)
	{
		return;
	}
	if(!params)
	{
		params = "";
	}
	if(!method)
	{
		method = "POST";
	}
	
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xmlhttp.open(method, "ajax/" + processor + ".php", true);
	xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttp.send(params);

	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			console.log(xmlhttp.responseXML);
			console.log(xmlhttp.responseText);
			eval(handler + "('" + xmlhttp.responseXML.documentElement + "')");
		}
	}
}
my log of responseXML looks like this:

Code: Select all

Document
<response>​
<status>​false​</status>​
<info>​invalid email address​</info>​
</response>​
my log of responseText looks like this:

Code: Select all

<?xml version="1.0" encoding="UTF-16"?>

<response>
	<status>false</status>
	<info>invalid email address</info>
</response>
My problem is in handle_email(ajax), the line

Code: Select all

var status = ajax.getElementsByTagName("status")[0].nodeValue;
throws an error:

Code: Select all

Uncaught TypeError: Object [object Element] has no method 'getElementsByTagName'
The script almost works, but how am I supposed to parse it properly?

Thanks,
Shawn

Re: ajax problem parsing responseXML

Posted: Wed Aug 04, 2010 12:57 pm
by shawngoldw
I have realised that

Code: Select all

eval(handler + "('" + xmlhttp.responseXML.documentElement + "')");
is not right. It is not being passed into the other function properly. responseXML should be an object, right? So how do I pass it properly. I tried removing the quotes inside of the brackets but that did not help:

Code: Select all

eval(handler + "(" + xmlhttp.responseXML.documentElement + ")");

Re: ajax problem parsing responseXML

Posted: Wed Aug 04, 2010 2:07 pm
by shawngoldw
fixed: instead of

Code: Select all

eval(handler + "(" + xmlhttp.responseXML.documentElement + ")");
use

Code: Select all

window[handler](xmlhttp.responseXML.documentElement);