簡體   English   中英

無法從Chrome中的xml源讀取文檔

[英]Document not reading from xml source in Chrome

我在Chrome中閱讀以下XML文檔時遇到問題-似乎沒有出現任何問題,但在Firefox中工作得很好。 這是瀏覽器兼容性問題嗎?

  <html>
  <head>
    <title>XML</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
    <script type='text/javascript'>

    function XMLload(){
        jQuery.post(url,function(data){getxml(data);},'xml');
    }

    function dataFromTag(node,t){
        var d=node.getElementsByTagName(t);
        if(d.length==0)
            return('');
        return(d[0].firstChild.nodeValue);
    }

    jQuery(document).ready(function(){XMLload();});

    // all the code above this line can be re-used without change
    // all the code below will need altering for a different document and different structure

    var url='cohort.xml';
    var xmlcohort;

    function getxml(xmldoc){
        xmlcohort = xmldoc.getElementsByTagName('student');

        var hstr = '<html><head><title></title></head><body>';
        hstr += '<p>Cohort:</p><form>';
        hstr += '<select size="' + xmlcohort.length + '" onclick="parent.student_info(this.selectedIndex);">';

        for( var i = 0; i < xmlcohort.length; i++ ) {
            hstr += '<option>' + dataFromTag(xmlcohort[i], 'name') + '</option>';
        }
        hstr += '</select></form></body></html>';

        with( document.getElementById('cohort').contentDocument ) {
            open();
            write(hstr);
            close();
        }
    }

    function student_info(idx) {
        var hstr = '<html><head><title></title></head><body><dl>';
        hstr += '<dt>Name</dt><dd>' + dataFromTag(xmlcohort[idx], 'name') + '</dd>';
        hstr += '<dt>Module</dt><dd>' + dataFromTag(xmlcohort[idx], 'module') + '</dd>';
        hstr += '<dt>Mark</dt><dd>' + dataFromTag(xmlcohort[idx], 'mark') + '</dd>';
        hstr += '</dl></body></html>';

        with( document.getElementById('student').contentDocument ) {
            open();
            write(hstr);
            close();
        }
    }

    </script>
  </head>
  <body>
    <iframe id='cohort' height='300' width='200'></iframe>
    <iframe id='student' height='300' width='200'></iframe>
    <p>Here is the <a href="cohort.xml">xml data</a></p>
  </body>
</html>

以下是cohort.xml的內容:

<cohort>
<student>
<name>Bob</name>
<module>COA122</module>
<mark>72</mark>
</student>
<student>
<name>Alice</name>
<module>COB250</module>
<mark>84</mark>
</student>
<student>
<name>Kate</name>
<module>COP180</module>
<mark>99</mark>
</student>
</cohort>

從硬盤驅動器運行該錯誤。 整個頁面無法加載-僅當我將其放在Web服務器上時才可以使用:

XMLHttpRequest cannot load file:///C:/Users/methuselah/Desktop/cohort/cohort.xml. Origin null is not allowed by Access-Control-Allow-Origin.

這是Chrome中的一個已知問題: http : //code.google.com/p/chromium/issues/detail?id=40787

如果這僅是為了測試和開發,則可以使用Chrome命令行標志來解決此問題:-- --allow-file-access-from-files 我剛剛對其進行了測試,它可以使您的頁面在Chrome瀏覽器中本地運行。

Windows: chrome.exe --allow-file-access-from-files

Mac: /Applications/Google\\ Chrome.app/Contents/MacOS/Google\\ Chrome --allow-file-access-from-files

Linux:Linux用戶不需要有關如何從命令行開始工作的說明! ;-)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM