zoukankan      html  css  js  c++  java
  • ajax

    XMLHttpRequest
    用于后台与服务器交换数据,不加载整个网页,对网页的某部分进行更新

    1.创建对象
    variable=new XMLHttpRequest();//创建一个对象
    IE5和IE6不支持需要用ActiveXObject("Microsoft.XMLHTTP")
    eg:
    var xmlhttp
    if(xindow.XMLHttpRequest){
    xmlhttp=new XMLHttpRequest();
    }else{
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    2.向服务器发送请求
    使用XMLHttpRequest对象的open()和send()方法
    open(method,url,async)
    //method:"POST"/"GET"
    //url:文件在服务器上的位置
    //async:true(同步)/false(异步)
    send(string)
    setRequestHeader(header,value)来添加 HTTP 头

    3.服务器响应
    使用XMLHttpResponse的responseText或responseXML属性
    responseText 获得字符串形式的响应数据。
    responseXML 获得 XML 形式的响应数据。

    4.
    onreadystatechange事件
    XMLHttpRequest对象的三个重要的属性:
    onreadystatechange
    存储函数,每当readyState改变时,就会触发 onreadystatechange 事件。
    readyState
    存有XMLHttpResponse的状态。从0-4变化
    0:请求未初始化
    1:服务器连接已建立
    2:请求已接收
    3:请求处理中
    4:请求已完成,且响应已就绪
    status
    200:OK
    404:未找到页面
    eg:
    xmlhttp.onreadystatechange=function(){
    if(xmlhttp.readyState=4 && xml.state==200){
    document.getElementById("div1").innerHTML=xmlhttp.responseText
    }
    }

    <!DOCTYPE>
    <html>
    <head>
    <script>
    function load(){
        var httpxml;
        //创建对象
        if(window.XMLHttpRequest){
            xmlhttp=new XMLHttpRequest();
        }else{
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        //向服务器发送请求
        xmlhttp.open("GET","a.txt",true);
        xmlhttp.send();
        //服务器响应
        xmlhttp.onreadystatechange=function(){
            if(xmlhttp.readyState==4 && xmlhttp.status==200){
                document.getElementById("div1").innerHTML=xmlhttp.responseText;
            }
        }
    }
    </script>
    <head>
    <body>
    <div id="div1">show</div>
    <button onclick="load()">click</button>
    
    </body>              
  • 相关阅读:
    .NET 开源GIS解决方案一 概述
    未能正确加载“Microsoft.VisualStudio.Editor.Implementation.EditorPackage”包
    C# Winform欢迎窗体实现()
    Log4Net使用指南(转)
    DotNet经典面试题(转载)
    这是一篇测试文章
    每天进步一点点
    博客园公告栏添加两个有趣的控件
    eclipse下查看maven下载的源码中文乱码问题
    WEB 基础知识(一)
  • 原文地址:https://www.cnblogs.com/mznsndy/p/11317943.html
Copyright © 2011-2022 走看看