<%-- Created by IntelliJ IDEA. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>这是一个ajax的demo</title> <script type="text/javascript"> window.onload = function () { document.getElementsByTagName("a")[0].onclick = function () { //1.创建XMLHttpRquest对象 var request = new XMLHttpRequest(); // 2.调用XMLHttpRequest对象的open()方法 request.open("GET","hello.txt"); // 3.调用对象的send方法 request.send(null); //为对象添加onreadystatechange响应函数 request.onreadystatechange = function () { // 4.判断响应是否可用,然后做响应的操作 if (request.readyState == 4) { if (request.status == 200 || request.status == 304) {
//弹出响应文本中的内容 alert(request.responseText); } } } return false; } } </script> </head> <body> <a href="hello.txt">点击我</a> </body> </html>