Schugs
Schugs

Reputation: 89

JavaScript AJAX call a PHP that returns XML

I am trying to to do an AJAX call from javascript to a PHP File, that will update a database, then return to the original java script function XML data. I think the disconnect i am having is getting the php to output correctly to be read as XML in the function. Please see the code i have below. ---EDIT--- Simpler code to find heart of issue.

--ANSWERED-- The PHP File has to have the header set as XML.

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

Java Script:

function AddNewUser(){
document.getElementById("overlay").innerHTML="<span>Started...</span>";
aj_test=new XMLHttpRequest();
aj_test.open("POST","test.php",true);
aj_test.setRequestHeader("Content-type","application/x-www-form-urlencoded");
alert("pre");
aj_test.onreadystatechange=function()
{
    if (aj_test.readyState==4 && aj_test.status==200)
    {
        XMLReturn=aj_test.responseXML;
        alert(XMLReturn.getElementsByTagName("title")[0].childNodes[0].nodeValue);
    }

}
aj_test.send("");

}

PHP:

$doc = new DOMDocument('1.0');
$doc->formatOutput = true;
$root = $doc->createElement('book');
$root = $doc->appendChild($root);
$title = $doc->createElement('title');
$title = $root->appendChild($title);
$text = $doc->createTextNode('This is the title');
$text = $title->appendChild($text);
echo $doc->saveXML();

Upvotes: 1

Views: 4148

Answers (1)

Schugs
Schugs

Reputation: 89

--ANSWERED-- The PHP File has to have the header set as XML.

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

Upvotes: 1

Related Questions