Ajax异步请求

一、异步请求的get请求的完整五步曲浏览器

        //1.0建立一个异步对象
            var xhr = new XMLHttpRequest();
            //2.0打开链接
            //           请求方式    请求路径     是否异步
            xhr.open("get", "/P02Time.ashx", true);
            //3.2设置请求报文头(清除缓存)
            xhr.setRequestHeader("If-Modified-Since", 0);//浏览器设置为当前请求不缓存
            //4.0设置回调函数(约定数据响应回来之后操做)
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    //获得从服务器拿回来的数据
                    var text = xhr.responseText;
                    document.getElementById("div").innerHTML = text;
                }
            }
            //5.0发送请求
            xhr.send();

 

二、异步请求的的post请求的完整五步曲缓存

        //1.0建立对象
            var xhr = new XMLHttpRequest();
            //2.0打开链接
            xhr.open("post", "/P02Time.ashx", true);
            //3.0设置请求报文头
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

            //4.0设置回调函数
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    var text = xhr.responseText;
                    document.getElementById("div").innerHTML = text;
                }
            }
            //5.0发送请求
            xhr.send("id=1&name=2");