打开相对文本文件javascript [英] open relative text file javascript
本文介绍了打开相对文本文件javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用javascript读取位于我网站相对路径中的文本文件?
How can I read a text file located in relative path of my site using javascript?
这是我一直试图做的,它说访问被拒绝:
This is what I been trying to do and it says access denied:
this.load = function(path){
if(root == null){
root = path;
}
var client = new XMLHttpRequest();
client.open('GET', "assets/myTextFile.txt");
client.onreadystatechange = function() {
alert(client.responseText);
};
client.send();
};
推荐答案
请尝试以下代码:
var xmlhttp;
if(window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
}
else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
alert(xmlhttp.responseText);
}
}
xmlhttp.open("GET","assets/myTextFile.txt",true);
xmlhttp.send();
这篇关于打开相对文本文件javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文