2011-08-24 84 views
0

我目前工作的一個項目試圖更新從我們的內部網服務器上的XML文件的網頁閱讀。做一些工作後,我想出了下面的代碼:的Javascript的XMLHttpRequest參數無效

// IE7+ 
if (window.XMLHttpRequest) { xmlhttp = new XMLHttpRequest(); } 
// IE6, IE5 
else { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } 
xmlhttp.open("GET", "VerifiedUrl+XML.xml", false); 
xmlhttp.send(); 
xmlDoc = xmlhttp.responseXML; 
if (xmlDoc.getElementsByTagName("CheckBox")[0].childNodes[0].nodeValue == "True"){ 
    document.getElementById("PartToUpdate").innerHTML = xmlDoc.getElementsByTagName("TextBox")[0].childNodes[0].nodeValue; 
} 

現在我測試過在我的本地這段代碼和它實際是從正確的文件讀取,顯示更新的信息,但是當我部署到內聯網,我得到一個「無效的參數」錯誤。 (XML文件本身已經部署並正確引用)。

編輯:我最近發現問題,因爲我引用的路徑顯然找不到文件本身。所以這帶來了另一個問題,有人也許能在闡明:

//When referencing a file within the same folder, it works correctly. 
xmlhttp.open("GET", "Sample.xml", false); 

//However, if I include the full path, it doesn't read correctly. (Double slashes to escape correctly) 
xmlhttp.open("GET", "\\\\Full\\Server\\Path\\Here\\Sample.xml", false); 

也許有人可以在此提供一些線索?

回答

0

這裏的道路是錯誤的:

xmlhttp.open("GET", "\\\\Full\\Server\\Path\\Here\\Sample.xml", false); 

您使用了錯誤類型爲斜槓互聯網的,他們將在文件系統上是正確的。它需要使用正斜槓。

xmlhttp.open("GET", "//Full/Server/Path/Here/Sample.xml", false); 
0

你檢查過same-origin policy

+0

原來兩者都是同一出處的一部分。我找到了一個解決方法,但爲了教育目的決定更新這個問題。 – Gobbledigook

1

應您的路徑是這樣的:

xmlhttp.open("GET","./Path/Here/books.xml", false); //for relative urls 

xmlhttp.open("GET","http://localhost/Path/Here/books.xml", false); //for absolute urls 

,如果它是一個非HTTP同步請求

var request = new XMLHttpRequest(); 

request.open('GET', 'file:///home/user/file.json', false); 

其不相似的系統路徑。