AlexC
AlexC

Reputation: 9661

jquery, XML to HTML

I have a question , about parsing xml.

I want to get some xml data to HTML, but I have a problem, I can get this data just from local xml, when I try to get data from external URL it doesn't work, for example from

http://www.w3schools.com/XML/plant_catalog.xml

This is jquery:

<script type="text/javascript">
        $(document).ready(function(){
            $.ajax({
                type: "GET",
                url: "http://www.w3schools.com/XML/plant_catalog.xml",
                dataType: "xml",
                success: function(xml) {
                    $(xml).find('CATALOG').each(function(){
                        var title = $(this).find('BOTANICAL').text();
                        var url = $(this).find('BOTANICAL').text();
                        $('<div class="items" id="link_"></div>').html('<a href="'+url+'">'+title+'</a>').appendTo('#page-wrap');

                    });
                }
            });
        });
     </script>

and HTML:

<div id="page-wrap">
        <h1>Reading XML with jQuery</h1>
     </div>

Thanks a lot !

Upvotes: 1

Views: 3841

Answers (2)

Alex
Alex

Reputation: 12433

You could also use a local file on your server as a wrapper to avoid these cross domain problems.

$.ajax({
  type: "GET",
  url: "catalog.php",
...

local catalog.php

<?php 
  header("Content-Type: text/xml");  
  echo file_get_contents("http://www.w3schools.com/XML/plant_catalog.xml"); 
?>

Notice that your server must have url fopen enabled for this to work.

Upvotes: 2

Gabriele Petrioli
Gabriele Petrioli

Reputation: 195982

You cannot request files from different domains for security reasons..

quoting http://api.jquery.com/jQuery.ajax/

When data is retrieved from remote servers (which is only possible using the script or jsonp data types)

Upvotes: 2

Related Questions