Ajax_使用XMLHttpRequest实现

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8" />
 5     <title>Document</title>
 6     <script type="text/javascript">
 7     window.onload = function () {
 8         // 1.获取a节点,并为其添加 onclick 相应函数
 9         document.getElementsByTagName("a")[0].onclick = function(){
10             //3.创建一个XMLHttpRequest 对象
11             var request = new XMLHttpRequest();
12
13             //4.准备发送请求的数据:url
14             var url = this.href + "?time=" + new Date();
15             var method = "GET";
16
17             //5.调用XMLHttpRequest 对象的 open 方法
18             request.open(method, url);
19
20             //6.XMLHttpRequest 对象的 send 方法
21             request.send(null);
22
23             //7.为XMLHttpRequest 对象添加 onreadystatechange 相应函数,由服务器触发
24             request.onreadystatechange = function () {
25
26                 //8.判断响应是否完成:XMLHttpRequest 对象的 readyState 属性值为 4 的时候
27                 if (request.readyState == 4) {
28                     //9.再判断响应是否可用: XMLHttpRequest 对象 status 属性值为 200
29                     if (request.status == 200 || request.status ==304) {
30                     //10.打印响应结果:responseText
31                     alert(request.responseText);
32                     }
33                 }
34             }
35
36             //2.取消a的默认行为
37             return false;
38         }
39     }
40     </script>
41 </head>
42 <body>
43     <a href="hellpAjax.txt">hellpAjax</a>
44 </body>
45 </html>
时间: 2024-08-27 02:46:47

Ajax_使用XMLHttpRequest实现的相关文章

Ajax_数据格式三大类

1.Ajax_数据格式_HTML 1 1 <!DOCTYPE html> 2 2 <html lang="en"> 3 3 <head> 4 4 <meta charset="utf-8"> 5 5 <script type="text/javascript"> 6 6 window.onload = function () { 7 7 var aNodes = document.get

html5 XMLHttpRequest 图片异步上传

<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <tilte>uploadFile</title> <script> function uploadFile(){ var formData = new FormData(); var files = document.getElementById('file').files; var file

AJAX 使用XMLHttpRequest对象发送和接受数据

XMLHttpRequest是XMLHttp组件的对象,通过这个对象,Ajax可以像桌面应用程序一样只与服务器进行数据层的交换,而不必每次都刷 新界面,也不必每次将数据处理的工作都交给服务器来做:这样既减轻了服务器负担又回忆了响应速度,缩短了用户的等待时间. XMLHttpRequest对象与Ajax       在Ajax应用程序中,XMLHttpRequest对象负责将用户信息以异步通信地发送到服务器端,并接收服务器响应信息和数据.       需要注意的是JavaScript本身并不具有向

XMLHttpRequest使用详解

1.什么是XMLHttpRequest XMLHttpRequest是一个浏览器接口,使得Javascript可以进行HTTP(S)通信,这就是我们熟悉的AJAX.早期,各个浏览器的实现都不同,HTML5之后,W3C进行了统一.2.XMLHttpRequest使用示例 <script type="text/javascript"> var xhr = new XMLHttpRequest(); xhr.timeout = 3000; xhr.ontimeout = func

jquery.js:8672 Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user&#39;s experience. For more help, check https://xhr.spec.whatwg.org/.

html5谷歌流浪器报错:jquery.js:8672 Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/. 解决方法: 所有的a标签加上:javascript:void(0) <a href="

XMLHttpRequest对象解读

<!DOCTYPE html> <html> <body> <script> function reqListener () { console.log(this); console.log(this.getAllResponseHeaders()); console.log(this.responseText); } var params = "lorem=ipsum&name=binny"; var oReq = new XM

Ajax中的XMLHttpRequest对象详解(转)

XMLHttpRequest对象是Ajax技术的核心.在Internet Explorer 5中,XMLHttpRequest对象以ActiveX对象引入,被称之为XMLHTTP,它是一种支持异步请求的技术.后来Mozilla.Netscape.Safari.Firefox和其他浏览器也提供了XMLHttpRequest类,虽然这些浏览器都提供了XMLHttpRequest类,但它们创建XMLHttpRequest类的方法并不相同.XMLHttpRequest使我们可以使用JavaScript向

Ajax异步XMLHttpRequest对象

示例Ajax: <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"

ajax中基本兼容各浏览器的XMLHttpRequest的创建

function createXHR(){ var xhr = null; if(window.XMLHttpRequest){//判断当前浏览器是否支持XMLHttpRequest xhr = new XMLHttpRequest(); }else if(window.XMLHttpRequest){//判断当前浏览器是否支持XMLHttpRequest,这是对于IE浏览器的判断 try{ xhr = new ActiveXObject("Msxml2.XMLHTTP");//IE6