我正在寫一個JavaScript程序來讀取本地XML文件中的數據。當我嘗試加載該文件,我從IE9一個錯誤說:在IE9中的XMLHTTP請求「訪問被拒絕」錯誤
SCRIPT5: Access is denied
當我嘗試加載在Chrome一樣,我得到一個錯誤說:
XMLHttpRequest cannot load file://... cross origin requests are only supported for HTTP
和
Uncaught NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'file...
以下是我的代碼:
<!DOCTYPE html>
<html>
<head>
<h1 id="si">Search Input</h1>
<button onclick="promptSearch()">Search By Vessel Name</button>
<h1 id="sr">Search Result</h1>
<script>
function loadXMLDoc(filename) {
if (window.XMLHttpRequest) {
xhttp=new XMLHttpRequest();
}
else { // for IE5 and IE6
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",filename,false);
xhttp.send();
return xhttp.responseXML;
}
function promptSearch() {
var prmpt = window.prompt("PLease enter vessel name:", "Vessel Name");
var search= prmpt.toLowerCase();
if(prmpt != null){
document.getElementById("si").innerHTML = search;
}
document.getElementById("sr").innerHTML = searchData();
}
function searchData() {
xmlDoc=loadXMLDoc("vesselData.xml");
}
</script>
</head>
</html>
您是否試圖從本地文件而不是從HTTP服務器運行此Javascript?你不能從本地文件使用AJAX。 – Barmar 2014-10-03 03:53:47