Javascript: simple xml request - Stack Overflow

I am learning this stuff so my code might not be pretty... but would appreciate some help :)I have not

I am learning this stuff so my code might not be pretty... but would appreciate some help :)

I have not written the following code but got it from somewhere else off the web:

function text_xml()
{
    realXmlUrl=".xml";
    http_request = false;
    http_request = new XMLHttpRequest();
    if (http_request.overrideMimeType)
    {
        http_request.overrideMimeType('text/xml');
    }

    http_request.onreadystatechange = this.response_xml;
    http_request.open('GET', realXmlUrl, true);
    http_request.send(null);
    xmlDoc = http_request.responseXML;
}

function response_xml()
{
    if (self.http_request.readyState == 4)
    {
        document.getElementById("ex").appendChild(document.createTextNode(" Done!"));
        getFruits(http_request.responseText);
    }
}

function getFruits(xml) {
  var fruits = xml.getElementsByTagName("fruits")[0];
  if (fruits) {
    var fruitsNodes = fruits.childNodes;
    if (fruitsNodes) {
      for (var i = 0; i < fruitsNodes.length; i++) {
        var name = fruitsNodes[i].getAttribute("name");
        var colour = fruitsNodes[i].getAttribute("colour");
        alert("Fruit " + name + " is coloured " + colour);
      }
    }
  }
}

And the error I am getting is:

Error: xml.getElementsByTagName is not a function

What am I doing wrong?

I am learning this stuff so my code might not be pretty... but would appreciate some help :)

I have not written the following code but got it from somewhere else off the web:

function text_xml()
{
    realXmlUrl="http://jumac./del_me_fruits.xml";
    http_request = false;
    http_request = new XMLHttpRequest();
    if (http_request.overrideMimeType)
    {
        http_request.overrideMimeType('text/xml');
    }

    http_request.onreadystatechange = this.response_xml;
    http_request.open('GET', realXmlUrl, true);
    http_request.send(null);
    xmlDoc = http_request.responseXML;
}

function response_xml()
{
    if (self.http_request.readyState == 4)
    {
        document.getElementById("ex").appendChild(document.createTextNode(" Done!"));
        getFruits(http_request.responseText);
    }
}

function getFruits(xml) {
  var fruits = xml.getElementsByTagName("fruits")[0];
  if (fruits) {
    var fruitsNodes = fruits.childNodes;
    if (fruitsNodes) {
      for (var i = 0; i < fruitsNodes.length; i++) {
        var name = fruitsNodes[i].getAttribute("name");
        var colour = fruitsNodes[i].getAttribute("colour");
        alert("Fruit " + name + " is coloured " + colour);
      }
    }
  }
}

And the error I am getting is:

Error: xml.getElementsByTagName is not a function

What am I doing wrong?

Share Improve this question edited Jul 30, 2011 at 0:06 carlosfigueira 87.4k14 gold badges136 silver badges174 bronze badges asked Jul 30, 2011 at 0:01 RyanRyan 10.1k23 gold badges68 silver badges103 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

responseText is a string, not an XML. Are you looking for responseXML?

Update

If your script is loaded from a different domain than the XML document you're loading (http://jumac./del_me_fruits.xml), then XMLHttpRequest will act differently depedning on the browser.

On IE 8, it will pop up a warning window plaining that "The page is accessing information that is not under its control. This poses a security risk. Do you want to continue?" if you click yes, then it will work correctly (i.e., the XML will load and the alerts for the fruits will be displayed).

On Chrome 12, however, it doesn't pop anything and it will say that "XMLHttpRequest cannot load http://jumac./del_me_fruits.xml. Origin http://localhost:54671 is not allowed by Access-Control-Allow-Origin." Because of this error, the responseXML property of the request object will be null and you'll see the error you have.

There are other questions regarding cross-domain XMLHttpRequest where you may find how to solve your issues, such as Cross-site XMLHttpRequest and http://code.google./chrome/extensions/xhr.html.

<body>
    <script type="text/javascript">
        function text_xml() {
            realXmlUrl = "http://jumac./del_me_fruits.xml";
            http_request = false;
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
            }

            http_request.onreadystatechange = this.response_xml;
            http_request.open('GET', realXmlUrl, true);
            http_request.send(null);
            xmlDoc = http_request.responseXML; // this doesn't have anything
        }

        function response_xml() {
            if (self.http_request.readyState == 4) {
                document.getElementById("ex").appendChild(document.createTextNode(" Done!"));
                getFruits(http_request.responseXML);
            }
        }

        function getFruits(xml) {
            var fruits = xml.getElementsByTagName("fruits")[0];
            if (fruits) {
                var fruitsNodes = fruits.childNodes;
                if (fruitsNodes) {
                    for (var i = 0; i < fruitsNodes.length; i++) {
                        var name = fruitsNodes[i].getAttribute("name");
                        var colour = fruitsNodes[i].getAttribute("colour");
                        alert("Fruit " + name + " is coloured " + colour);
                    }
                }
            }
        }
    </script>

    <input type="button" value="Click me" onclick="text_xml();" />
    <p><div id="ex"></div></p>
</body>

I usually love using a dictionary when working with any kind of transferring data across servers.

MkNxGn.pro provides a sleek way to make XML HTTP requests via MkNxGn Proquest.

Load Proquest, This can be separate from the code
<script src="https://mknxgn.pro/scripts/Proquest_Proquest-v1.0.js"></script>

<script> Proquest("POST", URL_HERE, DATA,<br> HEADERS, RType, Ignore JSON errors, Callback); </script>

That way you could easily write:

<script> Proquest("GET", "http://jumac./del_me_fruits.xml", Skip, {'Content-type': 'text/xml'}, 'response', false, function(resp) { resp.overrideMimeType('text/xml'); //Looks like you want it to be XML if its not. document.getElementById("ex").appendChild(document.createTextNode(" Done!")); getFruits(resp.responseXML); }); </script>

ignoring jason's edit to rewrite it better.

Consider using a javascript libary like jquery.

jquery ajax is pretty much self explaining and you don't have to mess with brower patibility. http://jquery./

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745194892a4616041.html

相关推荐

  • Javascript: simple xml request - Stack Overflow

    I am learning this stuff so my code might not be pretty... but would appreciate some help :)I have not

    1小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信