我在閱讀Chrome中的以下XML文檔時出現問題 - 沒有任何東西出現,但它在Firefox中完美運行。這是瀏覽器兼容性問題嗎?從Chrome中的xml源讀取文檔
<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.