0

brothers!! :) I am e beginner in ajax and I have a problem retrieving a note value from an response XML. what i am trying to do is (simple i think): with ajax I make a request for an XML file to a PHP page. I get the XML but i can not gat the notes values.

my simple XML:

<items>
  <item>
    <name>..</name>
    <address>..</address>
    <owner>..</owner>
  </item>
</items>

my PHP (that response):

<?php
    header('Content-Type: text/xml');
    $doc = new DOMDocument();
    $doc->formatOutput=true;
    $doc->load('data.xml');
    echo $doc->saveXML();
?>

my request function:

  function getUserParameters(){
  var xmlhttp = new XMLHttpRequest();
  xmlhttp.onreadystatechange=function Return(){
        if (xmlhttp.readyState==4 && xmlhttp.status==200){
            var result = xmlhttp.responseXML;
            var name= result.getElementsByTagName("name");
            var val = name[0].childNodes[0].nodeValue;
            alert(val);
        }
  }
xmlhttp.open("GET","request.php",true);
xmlhttp.send();
}

everything works ok, but when i try to alert(val) the string is null. Nothing appear! can anyone tell me what i do wrong please?
theks a lot for your time.

4

1 回答 1

0

First try calling the PHP response page directly, you should see the XML contents. If not, put the full path (not URL) of the XML file instead of data.xml.

This is not the ideal way of doing this though. You would be better to use the PHP reponse page to parse the data that you need put it into JSON and then use the Ajax to retrieve the values. See this page for more info: http://api.jquery.com/jQuery.getJSON/

于 2012-12-06T20:34:17.667 回答