Forum Moderators: open

Message Too Old, No Replies

request XML problem with AJAX

         

icpooreman

7:32 pm on Jan 9, 2006 (gmt 0)

10+ Year Member



Hey I'm new working with ajax and just want to grab some simple xml output but I can't seem to get it to work and can't seem to figure out why.

alright I'm posting to a php page which works fine the php page gets the input does some stuff then outputs some xml. The problem is at the end when I want to retrieve the XML output in the processrequestchange function when I ask for responseXML null is always returned.

Here's my javascript

if (window.XMLHttpRequest) {//mozilla
req = new XMLHttpRequest();
req.open("POST", url, true);
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
req.send(username + "&" + password);
req.onreadystatechange = processReqChange;
// branch for IE/Windows ActiveX version
}

function processReqChange(){
// only if req shows "complete"
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
response = req.responseXML.documentElement;//this line won't work always returns null
//var response = req.responseText; if I were to execute this line instead of the response xml I get the xml in text that I wanted to be output so I know my php file is outputting the right thing
} else {
alert("There was a problem retrieving the XML data:\n" + req.statusText);
}
}//end if ready
}//end processReqChange

Here's the XML that is output
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<response>
<method>checkName</method>
<result> result </result>
</response>

I think my problem might be my xml isn't validating because it doesn't have a doctype (however I can't get it to validate with one either) therefore when I request XML from the browser NULL is returned to it. If anyone has any ideas as to what I'm doing wrong it'd be appreciated.

Also if anyone knows of an example of xml that validates so I could test with that it would be appreciated

icpooreman

9:37 pm on Jan 9, 2006 (gmt 0)

10+ Year Member



Nevermind I figured it out it seems I forgot a line on the top of my php file

header('Content-Type: text/xml');

which was keeping it from working

execute

12:18 am on Jan 17, 2006 (gmt 0)

10+ Year Member



Thats true, but sometimes responseXML is BUGGY. The problem is it's hard to always get an XML content-type, so you can either set it using overrideMimeType which is for Mozilla only.

The best solution is using this tutorial just to be SAFE.
AJAX XML Tutorial [designplanet.biz]