简体   繁体   English

xhr xmlhttprequest无法在浏览器中使用?

[英]xhr xmlhttprequest won't work in browser?

I am using a browser to send XMLHttpRequest to read a file on the server: 我使用浏览器发送XMLHttpRequest来读取服务器上的文件:

//Read Text File
var xhr = new XMLHttpRequest();
xhr.open("GET","http://...........read.txt",false);
xhr.send(null); 
var fileContent = xhr.responseText;

But I got 但是我得到了

unknown error 未知错误

network error 网络错误

Any help is appreciated 任何帮助表示赞赏

check with status code condition like this: 检查状态代码条件如下:

    var xhr = new XMLHttpRequest();
xhr.open("GET","http://...........read.txt",false);
xhr.send(); 
console.log(xhr.responseText);

Remove false in xhr.open() function. xhr.open()函数中删除false Set onreadystatechange . 设置onreadystatechange You will get what you want! 你会得到你想要的!

 var xhr = new XMLHttpRequest(); xhr.open("GET","http://...........read.txt"); xhr.onreadystatechange = function () { if (this.readyState == 4 && this.status == 200) { console.log(xhr.responseText); } } xhr.send(); 

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

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