XMLHttpRequest无效参数

Javascript XMLHttpRequest Invalid Argument

本文关键字:参数 无效 XMLHttpRequest      更新时间:2023-09-26

我目前正在进行一个项目,试图更新从内部网服务器上的XML文件读取的页面。做了一些工作后,我想出了以下代码:

// 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文件本身已经部署,并且被正确引用)。

Edit:我最近发现了这个问题,因为我引用的路径显然找不到文件本身。这就引出了另一个问题,也许有人能解释清楚:

//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);  

也许有人能解释一下?

你的路径应该是这样的:

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);

和你的系统路径不一样

此处路径错误:

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

你在互联网上使用了错误的斜杠类型,它们在文件系统上是正确的。它需要使用正斜杠

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

您检查过同源策略了吗?