zoukankan      html  css  js  c++  java
  • ajax

    //1 创建xhr对象
    var xhr = createXHR();
    function createXHR()
    {
    var request;
    if (typeof (XMLHttpRequest) == "undefined") {
    //ie老版本中创建的方式
    request = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
    //支持标准的浏览器创建的方式
    request = new XMLHttpRequest();
    }
    return request;
    }
    window.onload = function () {
    document.getElementById("btn").onclick = function () {
    document.getElementById("d1").innerHTML = "正在努力加载..."
    //2 初始化xhr

                //ie中会读取缓存  
                //解决方法
                //1 加随机数
                //2 setRequestHeader   If-Modified-Since
                xhr.open("get", "01-get.ashx?_="+Math.random(), true);
                //xhr.setRequestHeader("If-Modified-Since", "0");
                //3 注册事件
                xhr.onreadystatechange = function () {
                    //判断readystate   是xhr的状态
                    //0   new 完了
                    //1   open完了
                    //2   send完了
                    //3  正在接收服务器返回的响应
                    //4  接收完成
                    if (xhr.readyState == 4) {
                        //判断服务器返回的状态码
                        if (xhr.status == 200) {
                            var r = xhr.responseText;
                            document.getElementById("d1").innerHTML = r;
                        } else {
                            document.getElementById("d1").innerHTML = "服务器内部错误";
                        }
                       
                    }
                }
                //4 发送请求
                xhr.send();
            }
        }
  • 相关阅读:
    MP教程-入门
    [15213] Assembly
    Crack the code interview
    [interview questions] 资料总结
    [Two Sigma OA] Longest Chain
    [Tow Sigma OA] friend cycles
    [security]
    [security] GNUpg
    [coursera] 面试前准备
    [coursera] [design] Hangman
  • 原文地址:https://www.cnblogs.com/poli/p/4296156.html
Copyright © 2011-2022 走看看