0

我下载了这段代码来测试尝试一些东西,但不知何故,尽管它在同一个文件夹中,但它似乎无法读取 xml。知道如何让它工作吗?

<!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>
4

1 回答 1

0

尝试通过 Apache 之类的 Web 服务器运行它。我不相信 XmlHttpRequest 跨浏览器对本地文件系统是可靠的。从技术上讲,它旨在向 Web 服务器发出 HTTP 请求。

看到这个答案

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

于 2013-03-18T01:50:10.943 回答