2017-06-24 10 views
-1

jQueryの初心者レベルは非常に新しいです。 .xmlファイルを取り込むようにコードを取得しようとしていますが、ロードできないと言います。私は、現時点では私のローカルホスト上で実行しています、私のコード:JQueryが読むことのできないtxtファイル

<!doctype html> 
<html> 
<head> 
<title>My Page</title> 
<meta http-equiv='Content-Type' content='text/html; charset=utf-8' /> 
<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0; minimum-scale=1.0; user-scalable=no; target-densityDpi=device-dpi" /> 
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css" /> 
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> 
<script src="http://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script> 
</head> 
<body> 

<div data-role="header"> 
     <h1>My Title</h1> 
    </div> <!--/header --> 

    <div data-role= "content"> 
     <ul id="myList"> 
     </ul> 
        <script type="text/javascript"> 

    console.log("starting....."); 

    var xml; 

    $(document).ready(function(){ 
     $.ajax({ 
      type:"GET", 
      url:"file:///C:/xammp/htdocs/MWA/lolcatz.xml", 
      dataType:"xml", 
      success:xmlParser 
     }); 
     });    

     function xmlParser(data) { 
      xml = data; 
      $(xml).find("kitteh").each(function() { 
       kitteh= $(this); 
       var name=$(kitteh).find("name").text(); 
       var src=$(kitteh).find("url").text(); 
       $("#myList").append('<li>'+ name+ '<li>'); 
      }); 
    } 
    </script> 
    </div><!-- /content --> 
    <div data-role="footer"> 
     <h4>My Footer</h4> 
    </div> <!-- /footer --> 
</div> 
<!-- /page --> 
</body> 
</html> 

私lolcatz.xmlは非常に基本的な

<LolCatz> 
    <kitteh> 
    <id>1</id> 
    <name>Ceiling Cat</name> 
    <url>http://upload.wikimedia.org/wikipedia/en/9/90/Ceiling_cat_no_text.gif 
    </url> 
    </kitteh> 
    <kitteh> 
    <id>2</id> 
    <name>Basement Cat</name> 
    <url> 
    http://i191.photobucket.com/albums/z37/beamerpi/TeeSting_s_Stuff/basement-cat-eats-souls.jpg 
    </url> 
    </kitteh> 
</LolCatz> 

である私は、このありえないが働いて、なぜ知りません、なぜそれが働かないのか教えてくれますか?ここで別の記事の方向に向けるようにしてください。 多くのありがとうございます。

+1

ディスクからファイルを使用することはできません。仮想ホストをインスタンシエートし、仮想ホストに入れます。 – FieryCat

答えて

0

ファイルのURLは、あなたのドキュメントルートがどこにあるか知っているあなたのXAMPの設定を確認する必要があります

http://localhost/MWA/lolcatz.xml 

する必要があります!

+0

このルートは正しいですが、私のコンソールツールのエラーは.... XMLHttpRequestはhttp://localhost/MWA/lolcatz.xmlをロードできません。要求されたリソースに「Access-Control-Allow-Origin」ヘッダーが存在しません。オリジン 'ヌル'はアクセスが許可されていません – awnstosh

+0

あなたのブラウザ「http://localhost/MWA/yourpage.html」でサーバからページを開く必要があります。ファイルとして開くと(ブラウザにfile://が表示されるはずです)、ブラウザセキュリティを無効にしない限り、常にエラーが発生します –

+0

ありがとうございますが、私は自分のサーバーにアップロードして動作します。 – awnstosh

関連する問題