简体   繁体   English

XMLHttpRequest()。send()在Chrome和Opera中不起作用

[英]XMLHttpRequest().send() not working in chrome and opera

The following javascript function works fine for IE, Safari and Firefox. 以下javascript函数可在IE,Safari和Firefox上正常运行。 But it fails in Chrome(33.0.) and Opera (16.0.1196). 但是在Chrome(33.0。)和Opera(16.0.1196)中失败。 Blank HTML page is displayed on loading. 加载时显示空白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. 仅打印“ a”和“ b”。 It does not continue after that. 此后不会继续。 Same result is observed if I use requ.send() or requ.send("") instead of requ.send(null). 如果我使用requ.send()或requ.send(“”)而不是requ.send(null),则会观察到相同的结果。

If I remove the statement requ.send(null), then 'null' value is printed for xmlDoc. 如果删除语句requ.send(null),则为xmlDoc打印“ null”值。 Still blank HTML loads. 仍为空白HTML加载。

Please let me know what is the right way to get this work on Chrome and Opera. 请让我知道在Chrome和Opera上进行这项工作的正确方法是什么。

Thanks 谢谢

SRB. SRB。

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 让我们看一下之前遇到的相同问题的问题: 跨源请求仅受HTTP支持,但不跨域

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

the call to the XMLHttpRequest.send method is Asynchronous so you need to modify the call a little bit. 对XMLHttpRequest.send方法的调用是异步的,因此您需要进行一些修改。 The modified code below will print the response content when the response is returned successfully: 成功返回响应后,下面修改的代码将打印响应内容:

 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. 更新:我没有注意到您使发送请求调用同步。

Edit You need to launch chrome with this parameter to be able to access local files 编辑您需要使用此参数启动chrome才能访问本地文件

--allow-file-access-from-files --allow-文件访问从-文件

ex: c:\\Browser\\chrome.exe --allow-file-access-from-files 例如:c:\\ Browser \\ chrome.exe --allow-file-access-from-files

I think that the problem is that you're passing null to the send() method. 我认为问题在于您正在将null传递给send()方法。 You are making a GET request, so you should call send without parameters. 您正在发出GET请求,因此应调用不带参数的send。 I think chrome throws an exception because of that. 我认为chrome因此会引发异常。 Just remove the null 只需删除null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM