问题描述
我该怎么做?我的文件名为 ajax_info.txt
,我的响应应该是一个自调用的 [handler] 函数。
解决方法
您可以使用以下代码执行此操作:
const xhttp = new XMLHttpRequest()
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("demo").innerHTML = this.responseText;
}
};
xhttp.open("GET","ajax_info.txt",true);
xhttp.send();
(function(){
document.getElementById("demo").innerHTML = xhttp.responseText;
})()
其中读取的文件为ajax_info.txt
,响应函数为最后三行。