• XMLHttpRequest


    AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML)。

    AJAX 不是新的编程语言,而是一种使用现有标准的新方法。

    AJAX 是在不重新加载整个页面的情况下,与服务器交换数据并更新部分网页的艺术。

    一.

    XMLHttpRequest对象-----AJAX 的基础

    XMLHttpRequest 用于在后台与服务器交换数据。这意味着可以在不重新加载整个网页的情况下,对网页的某部分进行更新。

    创建 XMLHttpRequest 对象

    代码:

    var xmlhttp;
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }

     服务器请求 

    .open(method,url,async,username, password

    • method:请求的类型;GET 或 POST和 HEAD。
    • url:请求地址。大多数浏览器实施了一个同源安全策略,并且要求这个 URL 与包含脚本的文本具有相同的主机名和端口。
    • async:true(异步)或 false(同步)
         false请求是同步的,后续对 send() 的调用将阻塞,直到响应完全接收。
         如果是 true 或省略,请求是异步的,且通常需要一个 onreadystatechange 事件。
    • username 和 password 参数是可选的,为 url 所需的授权提供认证资格。如果指定了,它们会覆盖 url 自己指定的任何资格。

     .send(string)

    string: 参数为请求体,作为一个字符串或者 Document 对象。如果请求体不是必须的话,这个参数就为 null

     简单的例子:

    xmlhttp.open("GET","demo_get.asp",true);
    xmlhttp.send();
    
    xmlhttp.open("POST","demo_post.asp",true);
    xmlhttp.send();
    
    xmlhttp.open("POST","ajax_test.asp",true);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
     //使用 setRequestHeader() 来添加 HTTP 头。然后在 send() 方法中规定您希望发送的数据:
    //setRequestHeader(header,value) 
    //header: 规定头的名称
    //value: 规定头的值
    
    xmlhttp.send("fname=Bill&lname=Gates");

     服务器响应

    如需获得来自服务器的响应,请使用 XMLHttpRequest 对象的 responseText 或 responseXML 属性。

    属性描述
    responseText 获得字符串形式的响应数据。
    responseXML 获得 XML 形式的响应数据。

    responseText 属性:如果来自服务器的响应并非 XML,请使用 responseText 属性。

    responseText 属性返回字符串形式的响应,因此可以这样使用:

    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;

    responseXML 属性:如果来自服务器的响应是 XML,而且需要作为 XML 对象进行解析,请使用 responseXML 属性:

    请求 books.xml 文件,并解析响应:

    xmlDoc=xmlhttp.responseXML;
    txt="";
    x=xmlDoc.getElementsByTagName("ARTIST");
    for (i=0;i<x.length;i++)
      {
      txt=txt + x[i].childNodes[0].nodeValue + "<br />";
      }
    document.getElementById("myDiv").innerHTML=txt;

    onreadystatechange 事件

    属性描述
    onreadystatechange 存储函数(或函数名),每当 readyState 属性改变时,就会调用该函数。
    readyState

    存有 XMLHttpRequest 的状态。从 0 到 4 发生变化。

    • 0: 初始化状态。XMLHttpRequest 对象已创建或已被 abort() 方法重置。         (请求未初始化,open还没有调用)
    • 1: 服务器连接已建立,open已经调用了
    • 2: 请求已接收,也就是接收到头信息了
    • 3: 请求处理中,也就是接收到响应主体了
    • 4: 请求已完成,且响应已就绪,也就是响应完成了
    status

    200: "OK"

    404: 未找到页面

    在 onreadystatechange 事件中,我们规定当服务器响应已做好被处理的准备时所执行的任务。

    当 readyState 等于 4 且状态为 200 时,表示响应已就绪:

    xmlhttp.onreadystatechange=function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
        }
      }

    总结,建立一个XMLHttpRequest 请求:

            var xhr = new XMLHttpRequest();
            xhr.open('GET', '', true);
            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    var data = xhr.responseText;
                }
            }
            xhr.send();

  • 相关阅读:
    连接池的实现 redis例子
    XSS的防御
    element-UI使用中:el-input type为textarea时@change无法触发?
    textarea高度自适应(转载)
    友盟统计单页面应用vue
    axios formData提交数据 && axios设置charset无效???
    解锁技能:sass + node-sass多页面应用编译(转载)
    css3新单位vw、vh、vmin、vmax的使用详解(转载)
    移动端bug集合
    Python3之Memcache使用
  • 原文地址:https://www.cnblogs.com/sunmarvell/p/7273106.html
Copyright © 2020-2023  润新知