zoukankan      html  css  js  c++  java
  • ajax小demo

    var oDiv = document.getElementById("div1");
                document.onclick = function(){

                    //var xhr = new XMLHttpRequest(); //创建XHR对象
                    var xhr;
                    if(window.XMLHttpRequest){
                        xhr = new XMLHttpRequest();//创建了一个XHR对象;
                    }else{
                        xhr = new ActiveXObject("MSxml12.XMLHTTP");//兼容IE6以下
                    }
                    
                    
                    xhr.open("get","demo.php",true); //准备发送请求 第一个参数:get/post 第二个:url 第三个:true(异步)

                    // 设置回调函数
                    xhr.onreadystatechange = function(){
                        if(xhr.readyState==4){
                            if(xhr.status==200){
                                // alert(xhr.responseText);
                                div1.innerHTML = xhr.responseText;
                            }else{
                                alert("error,restart");
                            }
                        }
                    }

                    // 发送请求
                     xhr.send(null); //get方式发送请求,send参数就是null
                }

                //兼容:
                // new AativeXObject("MSxml2.XMLHTTP") //IE6 IE5
                // new XMLHttpRequest(); //其他浏览器;
                //如果有就创建
                var request;
                if(window.XMLHttpRequest){
                    request = new XMLHttpRequest();//创建了一个XHR对象;
                }else{
                    request = new ActiveXObject("MSxml12.XMLHTTP");//兼容IE6以下
                }

  • 相关阅读:
    JavaScript与多线程的不解之缘!
    CSS居中的常用方式以及优缺点
    聊一聊Axios与登录机制
    熟悉而陌生API:Promise
    Cassandra数据类型:
    Cassandra 键空间(keyspace),表(table)
    Cassandra 配制 cassandra.yaml
    Linux 环境变量PS1设置
    添加sudo权限
    ssh免密码认证
  • 原文地址:https://www.cnblogs.com/yuejie/p/5986030.html
Copyright © 2011-2022 走看看