0

I downloaded this piece of code to test try out some stuff but somehow it doesn't seem to be able to read the xml although it is in the same folder. Any idea how to get it to work??

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" type="text/css" media="all" href="style.css" />
    <script type="text/javascript" src="jquery.js"></script>
    <title>Reading XML with jQuery</title>
     <script>
        $(document).ready(function(){
            $.ajax({
                type: "GET",
                url: "sites.xml",
                dataType: "xml",
                success: function(xml) {
                    $(xml).find('site').each(function(){
                        var id = $(this).attr('id');
                        var title = $(this).find('title').text();
                        var url = $(this).find('url').text();
                        $('<div class="items" id="link_'+id+'"></div>').html('<a href="'+url+'">'+title+'</a>').appendTo('#page-wrap');
                        $(this).find('desc').each(function(){
                            var brief = $(this).find('brief').text();
                            var long = $(this).find('long').text();
                            $('<div class="brief"></div>').html(brief).appendTo('#link_'+id);
                            $('<div class="long"></div>').html(long).appendTo('#link_'+id);
                        });
                    });
                }
            });
        });
     </script>
</head>
<body>
    <div id="page-wrap">
        <h1>Reading XML with jQuery</h1>
     </div>
</body>
</html>
Eric S
  • 1,363
  • 1
  • 10
  • 20
U.f.O
  • 299
  • 4
  • 7
  • 16
  • When you say download, are you saying that you're opening the page in your browser from your local file system? Or is this running through a web server? – Brandon Mar 18 '13 at 01:39
  • Hmm...url: "sites.xml" isn't working for me although I have placed the xml file within the same folder as the html. – U.f.O Mar 18 '13 at 01:40
  • @Brandon Opening in my browser from my local file. – U.f.O Mar 18 '13 at 01:41
  • 1
    @U.f.O Look at this thread. http://stackoverflow.com/a/5469527/1649198 AJAX is based on making HTTP requests, which technically don't make sense for local files. Browsers do not handle this consistently. – Brandon Mar 18 '13 at 01:42
  • It could work in Firefox, but chrome and IE won't allow it – Arun P Johny Mar 18 '13 at 03:25

1 Answers1

0

Try running it through a web server like Apache. I don't believe that XmlHttpRequest is reliable across browsers against the local file system. Technically it is meant to make HTTP requests to a web server.

See this SO answer

https://stackoverflow.com/a/5469527/1649198

Community
  • 1
  • 1
Brandon
  • 9,822
  • 3
  • 27
  • 37