读取外部文件的XMLHttpRequest
XMLHttpRequest to read an external file
我想通过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})
var xhr = new XMLHttpRequest();
xhr.open('POST', '/uploadFile');
var form = new FormData();
form.append('file', fileInput.files[0]);
xhr.send(form);
以前不可能用XMLHttpRequest对象上传二进制数据,因为它无法忍受使用FormData(无论如何,当时不存在FormData)对象。然而,由于新对象和XMLHttpRequest的第二个版本的到来,这个"壮举"现在很容易实现
这很简单,我们只需将File对象花在FormData对象上,并将其上传到
相关文章:
- XMLHttpRequest未返回值-状态202
- XMLHttpRequest在移动设备上的chrome上不起作用
- AJAX简单错误.XMLHttpRequest无法加载http://localhost/mpl/getPage.php.
- 检查xmlhttprequest问题的消息
- 主线程上的同步XMLHttpRequest已弃用
- 如何定期发出xmlhttprequest
- javascript第三个XMLHttpRequest被拒绝.但前两个是允许的
- 上传带有XMLHttprequest的文件-多部分/表单数据中缺少边界
- 对象#<XMLHttpRequest>没有方法'完成'
- 在XMLHttpRequest之后重新初始化jQuery
- 如何将GreaseMonkey中的XMLHttpRequest延迟到目标页面加载完成
- Javascript XMLHttpRequest——只有第一个POST请求有效
- XMLHttpRequest - difference between Chrome & Firefox
- IE上的新XMLHttpRequest()出现JS Ajax未指定错误
- 非常简单的XMLHttpRequest不起作用
- 如何使用XMLHttpRequest下载文件
- XMLHttpRequest在$.ajax工作的地方给出了CORS错误
- XMLHttpRequest.open()AJAX中的参数url
- XMLHTTPRequest脚本中没有internet连接和超时
- 为什么当async标志设置为false时,xmlhttprequest中的代码可以工作,而当它设置为true时却不能工作