zoukankan      html  css  js  c++  java
  • ajax中的一些小问题

    <html>
    <head>
    <script type="text/javascript">
    function loadXMLDoc()
    {
    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");
    }
    xmlhttp.onreadystatechange=function()
    {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
    }
    xmlhttp.open("POST","/ajax/demo_post2.asp",true);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xmlhttp.send("fname=Bill&lname=Gates");
    }
    </script>
    </head>
    <body>

    <h2>AJAX</h2>
    <button type="button" onclick="loadXMLDoc()">请求数据</button>
    <div id="myDiv"></div>

    </body>
    </html>

    上面是在w3school中的ajax中的实例,其中对于红字部分不太懂,记录一下:

    xmlhttp.readyState==4是因为在点击ajax的时候会调用四次function()函数,

    在第四次才是真正的ajax成功接收数据所以为了节省没必要的步骤也是为了节省资源(function()同样是调用了四次)

    xmlhttp.status==200是因为http状态码200是接收成功,让其响应成功,也同样是为了节省资源。

    有时候在send()中写上null是因为不写的话有的浏览器会出错,写了不会出错,因此才写的

    post请求中send()中是应该写请求的参数的

  • 相关阅读:
    VBA宏 合并EXCEL
    Opensource开源精神
    Redisd VS Memcached
    google-analytics.com
    Sybase PowerDesign 导入数据库结构formSqlserver
    Session Sticky About Nginx
    网站应用程式架构
    MapReduce形象总结
    Ubuntu升级内核
    ASP.NET5 MVC6入门教学之一(自己动手)
  • 原文地址:https://www.cnblogs.com/toomucherror/p/10529677.html
Copyright © 2011-2022 走看看