javascript XMLHttpRequest().send() 在 chrome 和 opera 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22140614/
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().send() not working in chrome and opera
提问by srb
The following javascript function works fine for IE, Safari and Firefox. But it fails in Chrome(33.0.) and Opera (16.0.1196). Blank HTML page is displayed on loading.
以下 javascript 函数适用于 IE、Safari 和 Firefox。但它在 Chrome(33.0.) 和 Opera (16.0.1196) 中失败。加载时显示空白 HTML 页面。
function readTestXMLFile() {
if (window.ActiveXObject) {
var xmlDoc = new ActiveXObject('Microsoft.XMLDOM');
xmlDoc.async = 'false';
xmlDoc.load('test.xml');
}
else {
var requ = new XMLHttpRequest();
alert("a");
requ.open("GET", "test.xml", false);
alert("b");
requ.send(null); //This line is not working in chrome and opera
alert("c");
var xmlDoc = requ.responseXML;
alert(xmlDoc);
alert("d");
}
return xmlDoc;
}
Only 'a' and 'b' gets printed. It does not continue after that. Same result is observed if I use requ.send() or requ.send("") instead of requ.send(null).
只打印 'a' 和 'b'。之后就不会继续了。如果我使用 requ.send() 或 requ.send("") 而不是 requ.send(null),则会观察到相同的结果。
If I remove the statement requ.send(null), then 'null' value is printed for xmlDoc. Still blank HTML loads.
如果我删除语句 requ.send(null),则为 xmlDoc 打印 'null' 值。仍然空白的 HTML 加载。
Please let me know what is the right way to get this work on Chrome and Opera.
请让我知道在 Chrome 和 Opera 上进行这项工作的正确方法是什么。
Thanks
谢谢
SRB.
SRB。
采纳答案by Rami
the call to the XMLHttpRequest.send method is Asynchronous so you need to modify the call a little bit. The modified code below will print the response content when the response is returned successfully:
对 XMLHttpRequest.send 方法的调用是异步的,因此您需要稍微修改一下调用。下面修改后的代码会在响应成功返回时打印响应内容:
requ.addEventListener("load", function(e) {
alert(req.responseText);
}, false)
requ.send(null);
Update:I didn't notice that you made the send request call synchronous.
更新:我没有注意到您使发送请求调用同步。
EditYou need to launch chrome with this parameter to be able to access local files
编辑您需要使用此参数启动chrome才能访问本地文件
--allow-file-access-from-files
--allow-file-access-from-files
ex: c:\Browser\chrome.exe --allow-file-access-from-files
例如:c:\Browser\chrome.exe --allow-file-access-from-files
回答by Mathias
Your error message suggest that you are trying to access a local file which is treated as "Cross origin request"if you try and run local server it should work.
您的错误消息表明您正在尝试访问被视为“跨源请求”的本地文件,如果您尝试运行它应该可以工作的本地服务器。
Take a look at this previously asked question with the same problem: Cross origin requests are only supported for HTTP but it's not cross-domain
看一下之前提出的同样问题的问题: Cross origin requests are only supported for HTTP but it's not cross-domain
Then you would access http://localhost/.../test.xml
instead of c:/localhost/.../test.xml
然后你会访问http://localhost/.../test.xml
而不是c:/localhost/.../test.xml
You can also set a flag for Chrome to allow local files to request local files: -allow-file-access-from-files
您还可以为 Chrome 设置一个标志以允许本地文件请求本地文件: -allow-file-access-from-files
回答by Oscar Paz
I think that the problem is that you're passing nullto the send() method. You are making a GET request, so you should call send without parameters. I think chrome throws an exception because of that. Just remove the null
我认为问题在于您将null传递给 send() 方法。您正在发出 GET 请求,因此您应该调用不带参数的 send。我认为 chrome 会因此引发异常。只需删除空值