Javascript XMLHttpRequest 无效参数

发布于 2024-12-01 09:47:34 字数 1071 浏览 0 评论 0原文

我目前正在开发一个项目,尝试更新从 Intranet 服务器上的 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;
}

现在我已经在本地主机上测试了这段代码,它实际上从正确的文件中读取,显示更新的信息,但是当我将其部署到 Intranet 时,我得到一个“无效参数”错误。 (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);  

也许有人可以对此做出一些解释?

I'm currently working on a project trying to update a page reading from an XML file on our intranet server. After doing some working I came up with the following code:

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

Now I've tested this code on my localhost and it does in fact read from the correct file, displaying the updated information, but when I deploy it to the intranet, I get an "Invalid Argument" error. (The XML file itself has been deployed and is referencing correctly).

Edit: I've recently found the problem, in that the path I was referencing apparently couldn't find the file itself. So that brings up another question that someone might be able to shed light on:

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

Perhaps someone could shed some light on this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅笑轻吟梦一曲 2024-12-08 09:47:34

你的路径应该是这样的:

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

它与你的系统路径不相似。

should your path be something like this:

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

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

and if its a non-http synchronous request

var request = new XMLHttpRequest();

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

its not similar to your system path.

梦里梦着梦中梦 2024-12-08 09:47:34

这里的路径是错误的:

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

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

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

The path here is wrong:

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

You are using the wrong type of slashes for the internet, they would be correct on a file system. It needs to use a forward slash.

xmlhttp.open("GET", "//Full/Server/Path/Here/Sample.xml", false);  
掩耳倾听 2024-12-08 09:47:34

您检查过同源政策吗?

Have you checked the same-origin policy?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文