gpt4 book ai didi

javascript - XMLHttpRequest 读取外部文件

转载 作者:可可西里 更新时间:2023-11-01 16:19:03 26 4
gpt4 key购买 nike

我想通过 JavaScript(在客户端浏览器上运行)检索文本文件中包含的数据(从给定的 URL)。

到目前为止,我已经尝试了以下方法:

var xmlhttp, text;
xmlhttp = new XMLHttpRequest();
xmlhttp.open('GET', 'http://www.example.com/file.txt', false);
xmlhttp.send();
text = xmlhttp.responseText;

但它只适用于 Firefox。有没有人有任何建议使它在每个浏览器中都能正常工作?

谢谢

最佳答案

IT 在 IE 旧版本中使用 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 工作。 Chrome、Firefox 和所有合理的浏览器都使用 xhr

坦率地说,如果你想要跨浏览器兼容性,使用jquery

那里很简单:

var text="";
$.get(url, function(data){text=data;//Do something more with the data here. data variable contains the response})

关于javascript - XMLHttpRequest 读取外部文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9155960/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com