• AJAX基础语法


    在这里插入图片描述

    AJAX是基于现有的Internet标准,并且联合使用它们:

    • XMLHttpRequest 对象 (异步的与服务器交换数据)
    • JavaScript/DOM (信息显示/交互)
    • CSS (给数据定义样式)
    • XML (作为转换数据的格式)

    Google Suggest 使用 AJAX 创造出动态性极强的 web 界面:当您在谷歌的搜索框输入关键字时,JavaScript 会把这些字符发送到服务器,然后服务器会返回一个搜索建议的列表

    创建 XMLHttpRequest 对象

    <script>
    function loadXMLDoc()
    {
    	var xmlhttp;
    	if (window.XMLHttpRequest)
    	{
    		//  IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
    		xmlhttp=new XMLHttpRequest();
    	}
    	else
    	{
    		// IE6, IE5 浏览器执行代码
    		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    	}
    	xmlhttp.onreadystatechange=function()
    	{
    		if (xmlhttp.readyState==4 && xmlhttp.status==200)
    		{
    			document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    		}
    	}
    	xmlhttp.open("GET","/try/ajax/ajax_info.txt",true);
    	xmlhttp.send();
    }
    script>
    head>
    <body>
    
    <div id="myDiv"><h2>使用 AJAX 修改该文本内容h2>div>
    <button type="button" onclick="loadXMLDoc()">修改内容button>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    向服务器发送请求

    XMLHttpRequest open() send()

    xmlhttp.open("GET","ajax_info.txt",true);
    xmlhttp.send();
    
    • 1
    • 2

    GET 请求

    为了避免得到缓存,请向 URL 添加一个唯一的 ID:

    xmlhttp.open("GET","/try/ajax/demo_get.php?t=" + Math.random(),true);
    xmlhttp.send();
    
    • 1
    • 2

    POST 请求

    xmlhttp.open("POST","/try/ajax/demo_post.php",true);
    xmlhttp.send();
    
    • 1
    • 2

    如果需要像 HTML 表单那样 POST 数据,请使用 setRequestHeader() 来添加 HTTP 头。然后在 send() 方法中规定您希望发送的数据:

    <script>
    function loadXMLDoc()
    {
      var xmlhttp;
      if (window.XMLHttpRequest)
      {
        // IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
        xmlhttp=new XMLHttpRequest();
      }
      else
      {
        // IE6, IE5 浏览器执行代码
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      xmlhttp.onreadystatechange=function()
      {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
          document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
        }
      }
      xmlhttp.open("POST","/try/ajax/demo_post2.php",true);
      xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
      xmlhttp.send("fname=Henry&lname=Ford");
    }
    script>
    head>
    <body>
    
    <h2>AJAXh2>
    <button type="button" onclick="loadXMLDoc()">请求数据button>
    <div id="myDiv">div>
     
    body>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    服务器 响应

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

    responseText 属性

    <script>
    function loadXMLDoc()
    {
    	var xmlhttp;
    	if (window.XMLHttpRequest)
    	{
    		//  IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
    		xmlhttp=new XMLHttpRequest();
    	}
    	else
    	{
    		// IE6, IE5 浏览器执行代码
    		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    	}
    	xmlhttp.onreadystatechange=function()
    	{
    		if (xmlhttp.readyState==4 && xmlhttp.status==200)
    		{
    			document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    		}
    	}
    	xmlhttp.open("GET","/try/ajax/ajax_info.txt",true);
    	xmlhttp.send();
    }
    script>
    head>
    <body>
    
    <div id="myDiv"><h2>使用 AJAX 修改该文本内容h2>div>
    <button type="button" onclick="loadXMLDoc()">修改内容button>
    
    body>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    responseXML 属性⭐

    <script>
    function loadXMLDoc()
    {
      var xmlhttp;
      var txt,x,i;
      if (window.XMLHttpRequest)
      {
        // IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
        xmlhttp=new XMLHttpRequest();
      }
      else
      {
        // IE6, IE5 浏览器执行代码
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      xmlhttp.onreadystatechange=function()
      {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
          xmlDoc=xmlhttp.responseXML;
          txt="";
          x=xmlDoc.getElementsByTagName("ARTIST");
          for (i=0;i<x.length;i++)
          {
            txt=txt + x[i].childNodes[0].nodeValue + "
    "
    ; } document.getElementById("myDiv").innerHTML=txt; } } xmlhttp.open("GET","cd_catalog.xml",true); xmlhttp.send(); }
    script> head> <body> <h2>我收藏的 CD :h2> <div id="myDiv">div> <button type="button" onclick="loadXMLDoc()">获取我的 CDbutton> body>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    onreadystatechange 事件

    <script>
    function loadXMLDoc()
    {
    	var xmlhttp;
    	if (window.XMLHttpRequest)
    	{
    		//  IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
    		xmlhttp=new XMLHttpRequest();
    	}
    	else
    	{
    		// IE6, IE5 浏览器执行代码
    		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    	}
    	xmlhttp.onreadystatechange=function()
    	{
    		if (xmlhttp.readyState==4 && xmlhttp.status==200)
    		{
    			document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    		}
    	}
    	xmlhttp.open("GET","/try/ajax/ajax_info.txt",true);
    	xmlhttp.send();
    }
    script>
    <div id="myDiv"><h2>使用 AJAX 修改该文本内容h2>div>
    <button type="button" onclick="loadXMLDoc()">修改内容button>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    使用回调函数

    回调函数是一种以参数形式传递给另一个函数的函数。

    如果您的网站上存在多个 AJAX 任务,那么您应该为创建 XMLHttpRequest 对象编写一个标准的函数,并为每个 AJAX 任务调用该函数。

    该函数调用应该包含 URL 以及发生 onreadystatechange 事件时执行的任务(每次调用可能不尽相同):

    <script>
    var xmlhttp;
    function loadXMLDoc(url,cfunc)
    {
    if (window.XMLHttpRequest)
      {// IE7+, Firefox, Chrome, Opera, Safari 代码
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// IE6, IE5 代码
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    xmlhttp.onreadystatechange=cfunc;
    xmlhttp.open("GET",url,true);
    xmlhttp.send();
    }
    function myFunction()
    {
    	loadXMLDoc("/try/ajax/ajax_info.txt",function()
    	{
    		if (xmlhttp.readyState==4 && xmlhttp.status==200)
    		{
    			document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    		}
    	});
    }
    script>
    head>
    <body>
    
    <div id="myDiv"><h2>使用 AJAX 修改文本内容h2>div>
    <button type="button" onclick="myFunction()">修改内容button>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    ASP/PHP 实例

    function showHint(str)
    {
        var xmlhttp;
        if (str.length==0)
        { 
            document.getElementById("txtHint").innerHTML="";
            return;
        }
        if (window.XMLHttpRequest)
        {
            // IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
            xmlhttp=new XMLHttpRequest();
        }
        else
        {
            // IE6, IE5 浏览器执行代码
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
                document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET","/try/ajax/gethint.php?q="+str,true);
        xmlhttp.send();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    如果输入框为空 str.length==0,则该函数清空 txtHint 占位符的内容,并退出函数。
    如果输入框不为空,showHint() 函数执行以下任务:
    创建 XMLHttpRequest 对象
    当服务器响应就绪时执行函数
    把请求发送到服务器上的文件
    请注意我们向 URL 添加了一个参数 q (带有输入框的内容)

    AJAX Database 实例⭐

    function showCustomer(str)
    {
      var xmlhttp;    
      if (str=="")
      {
        document.getElementById("txtHint").innerHTML="";
        return;
      }
      if (window.XMLHttpRequest)
      {
        // IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
        xmlhttp=new XMLHttpRequest();
      }
      else
      {
        // IE6, IE5 浏览器执行代码
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      xmlhttp.onreadystatechange=function()
      {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
          document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
        }
      }
      xmlhttp.open("GET","/try/ajax/getcustomer.php?q="+str,true);
      xmlhttp.send();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    showCustomer() 函数执行以下任务:

    检查是否已选择某个客户
    创建 XMLHttpRequest 对象
    当服务器响应就绪时执行所创建的函数
    把请求发送到服务器上的文件
    请注意我们向 URL 添加了一个参数 q (带有输入域中的内容)

    XML 实例

    function loadXMLDoc() {
      var xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
        myFunction(this);
        }
      };
      xhttp.open("GET", "cd_catalog.xml", true);
      xhttp.send();
    }
    function myFunction(xml) {
      var i;
      var xmlDoc = xml.responseXML;
      var table="ArtistTitle";
      var x = xmlDoc.getElementsByTagName("CD");
      for (i = 0; i <x.length; i++) { 
        table += "" +
        x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue +
        "" +
        x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue +
        "";
      }
      document.getElementById("demo").innerHTML = table;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    当用户点击上面的"获取我收藏的 CD"这个按钮,就会执行 loadXMLDoc() 函数。

    loadXMLDoc() 函数创建 XMLHttpRequest 对象,添加当服务器响应就绪时执行的函数,并将请求发送到服务器。

    当服务器响应就绪时,会构建一个 HTML 表格,从 XML 文件中提取节点(元素),最后使用 XML 数据的 填充 id=“demo” 的表格元素
    在这里插入图片描述

  • 相关阅读:
    【开源】加油站管理系统 JAVA+Vue.js+SpringBoot+MySQL
    arrow的使用
    为什么虚拟dom会提高性能?
    [数据结构] 顺序表 + 多种链表
    使用Locust进行性能测试
    71.【MySQL-二刷】
    Camtasia Studio 2024软件最新版下载【安装详细图文教程】
    Cleanmymac X绿色中文苹果系统清理软件
    oracle查询数据库参数sql语句
    (数据科学学习手札152)geopandas 0.13版本更新内容一览
  • 原文地址:https://blog.csdn.net/qq_44174346/article/details/126164135