简体   繁体   English

本地.txt文件上的XMLHttpRequest()在除Firefox以外的所有浏览器中返回空的responseText

[英]XMLHttpRequest() on a local .txt file is returning an empty responseText in all browsers except Firefox

For some reason my XMLHttpRequest().responseText is empty when trying to pull from a file contained in the same subdirectory. 由于某种原因,当尝试从同一子目录中包含的文件中提取文件时,我的XMLHttpRequest()。responseText为空。

I'm not checking for XMLHttpRequest().status == 200 , because from what I have been able to discover during my searches, status will always be 0 if the requested file is local - an issue I had encountered minutes before when I was checking for XMLHttpRequest().readyState == 4 && XMLHttpRequest().status == 200 . 我没有检查XMLHttpRequest()。status == 200 ,因为从我在搜索过程中发现的内容来看,如果请求的文件是本地文件,状态将始终为0-这是我在几分钟前遇到的一个问题检查XMLHttpRequest()。readyState == 4 && XMLHttpRequest()。status == 200 This is due to not going through a webserver. 这是由于未通过网络服务器。

The file (data.txt) contains plain text, but the responseText is always returned empty, and I cannot figure out why. 该文件(data.txt)包含纯文本,但是responseText始终返回为空,我无法弄清原因。 Any help will be greatly appreciated! 任何帮助将不胜感激!

UPDATE: Seems it works in Firefox, but not Chrome (with which I was using to test), or IE. 更新:似乎可以在Firefox中使用,但不能在Chrome(我用来测试的浏览器)或IE中使用。 These browser aren't putting data into responseText, so does anyone know why, or better, how to rectify it? 这些浏览器没有将数据放入responseText,所以有人知道为什么,或者更好地纠正它吗?

function loadData()
{
    var request = new XMLHttpRequest();
    request.open("GET", "data.txt", true);
    request.onreadystatechange = function()
    {
        if (request.readyState == 4)
            useData(request.responseText);
    }
    request.send(null);
}

This is by design to prevent javascript exploits shared as for example attachments in mail files (as something along the lines of domains doesn't exist for desktop application and there is just a single filesystem). 这是设计使然,以防止诸如邮件文件中的附件之类的javascript漏洞被共享(因为桌面应用程序不存在域界限,只有一个文件系统)。 For development you can disable this security feature in chrome by using the --disable-web-security flag, but in general you just have to accept that that's impossible. 为了进行开发,您可以使用--disable-web-security标志在chrome中禁用此安全功能,但通常您只需要接受那是不可能的。

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

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