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以下
                }

  • 相关阅读:
    final/override控制
    高效遍历图像
    快速初始化成员变量
    C++ boost.python折腾笔记
    百亿数据毫秒响应级交易系统读写分离存储数据设计
    解决VS2010子目录中的.cpp文件引用上一级目录的stdafx.h找不到定义的问题
    生产应用常见坑
    spring AOP应用
    springmvc No mapping found for HTTP request with URI in Dispatc
    myeclipse使用maven插件进行maven install时报错check $m2_home environment variable and mvn script match
  • 原文地址:https://www.cnblogs.com/yuejie/p/5986030.html
Copyright © 2011-2022 走看看