2013-03-18 101 views
0

我下載了這段代碼來測試一些東西,但不知何故它似乎無法讀取xml,儘管它在同一個文件夾中。任何想法如何讓它工作?混淆了這個url請求

<!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> 
+0

當你說下載的,你說你是在爲你的瀏覽器從本地文件系統中打開網頁?或者這是通過網絡服務器運行? – Brandon 2013-03-18 01:39:07

+0

嗯......網址:「sites.xml」對我來說不起作用,儘管我已經將xml文件放在與html相同的文件夾中。 – 2013-03-18 01:40:41

+0

@Brandon從我的本地文件在我的瀏覽器中打開。 – 2013-03-18 01:41:22

回答

0

嘗試通過像Apache這樣的Web服務器來運行它。我不相信XmlHttpRequest在瀏覽器上對本地文件系統是可靠的。從技術上講,它意味着向Web服務器發出HTTP請求。

看到這個蘇答案

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