Javascript XMLHttpRequest 从远程主机获取 HTTP 响应
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2060551/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
XMLHttpRequest to get HTTP response from remote host
提问by Maxim Veksler
Why the following code Based on Mozilla exampledoes not work? Tried with Firefox 3.5.7 & Chrome.
为什么以下基于 Mozilla 示例的代码不起作用?尝试使用 Firefox 3.5.7 和 Chrome。
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
</head>
<body>
</body>
<script>
var req = new XMLHttpRequest();
req.open('GET', 'http://www.mozilla.org/', false);
req.send();
if(req.status == 200) {
alert(req.responseText);
}
</script>
</html>
Please that the browser is pulling the html from local disk (file:///C:/Users/Maxim%20Veksler/Desktop/XMLHTTP.html)
请确保浏览器从本地磁盘中提取 html (file:///C:/Users/Maxim%20Veksler/Desktop/XMLHTTP.html)
On Firefox it gives the following error:
在 Firefox 上,它给出以下错误:
uncaught exception: [Exception... "Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [nsIXMLHttpRequest.send]" nsresult: "0x80004005 (NS_ERROR_FAILURE)" location: "JS frame :: file:///C:/Users/Maxim%20Veksler/Desktop/XMLHTTP.html :: <TOP_LEVEL> :: line 10" data: no]
What am I doing wrong? I want to submit a request to remote host and alert the result (later to added into a div).
我究竟做错了什么?我想向远程主机提交请求并提醒结果(稍后添加到 div 中)。
回答by Daniel Vassallo
Your browser is preventing cross-site scripting. You have to use a relative path, otherwise most browsers will simply return an error or an empty responseText.
您的浏览器正在阻止跨站点脚本。您必须使用相对路径,否则大多数浏览器只会返回错误或空的 responseText。
The following Stack Overflow post is probably also related to your problem:
以下 Stack Overflow 帖子可能也与您的问题有关:
回答by Ryan Joy
I'm also assuming that you've opened your HTML test page directly in the browser judging by your reference to file:///.... For XMLHttpRequestcalls, you need to serve the HTML from a server. Try something like xampp (http://www.apachefriends.org/en/xampp.html) to get a local server up and running and then run your test from http://localhost/XMLHTTP.html.
我还假设您已经直接在浏览器中打开了 HTML 测试页,根据您对file:///.... 对于XMLHttpRequest调用,您需要从服务器提供 HTML。尝试使用类似 xampp ( http://www.apachefriends.org/en/xampp.html) 的方法来启动并运行本地服务器,然后从http://localhost/XMLHTTP.html.
Note, this does not solve your same-origin problem, but this would allow the following code to work:
请注意,这并不能解决您的同源问题,但这将允许以下代码工作:
<script>
var req = new XMLHttpRequest();
req.open('GET', '/myTestResponse.html', false);
req.send();
if(req.status == 200) {
alert(req.responseText);
}
</script>
回答by Andras Zoltan
Security issue no?
安全问题没有?
Presumably firefox is preventing the local file from talking to a remote host?
据推测,firefox 正在阻止本地文件与远程主机通信?
Scouting round the net - found this. Try adding this to the start of your script:
在网上侦察 - 发现了这个。尝试将其添加到脚本的开头:
try {
netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
} catch (e) {
alert("Permission UniversalBrowserRead denied.");
}
Can't guarantee this'll work; because ultimately what you're trying to do is enter a security hole that browsers have been specifically coded to have plugged up (cross-domain requests).
不能保证这会起作用;因为最终您要做的是进入一个安全漏洞,浏览器已被专门编码为已插入(跨域请求)。
There are some special scenarios where it can be switched on, though, usually at the user's discretion.
但是,在某些特殊情况下可以打开它,通常由用户自行决定。
回答by CalebD
You cannot make requests across domains, even with local files.
您不能跨域发出请求,即使使用本地文件也是如此。
https://developer.mozilla.org/en/Same_origin_policy_for_JavaScript
https://developer.mozilla.org/en/Same_origin_policy_for_JavaScript
Unlessyou are developing an extension, which does not have the same restrictions as a web page.
除非您正在开发一个扩展,它没有与网页相同的限制。

