AJAX(Asynchronous Javascript and XML),是一种创建交互式网页应用的网页开发技术。AJAX是一种用于创建快速动态网页的技术,使网页在不需要重新加载整个网页的情况下,对网页的某个部分进行更新。
实现AJAX有两种方式:一种是原生的JS代码,另一种是借助JQuery等第三方插件来实现。以下是使用原生JavaScript代码来实现AJAX:
let ajax1, fileText;
let textArea = document.getElementById("textArea");
if (window.XMLHttpRequest) {
ajax1 = new XMLHttpRequest();
} else {
ajax1 = new ActiveXObject("Microsoft.XMLHTTP");
}
ajax1.open('GET', 'text1.txt', true);
ajax1.send();
ajax1.onreadystatechange = function() {
if (ajax1.readyState==4) {
if(ajax1.status==200) {
fileText = ajax1.responseText;
textArea.innerHTML = fileText;
}
}
}