zoukankan      html  css  js  c++  java
  • php javascript的ajax

    先说基础一点的get类型的ajax

    function loadXMLDoc()
    {
    var xmlhttp;
    //首先判断浏览器是否支持xmlhttprequest,因为ie56不是这个对象,是activexobject
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari xmlhttp=new XMLHttpRequest(); } else {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }
    //然后是当readystate改变的时候,判断状态是否正常,如果正常说明请求完成了,就进行请求后的操作 xmlhttp.onreadystatechange
    =function() { if (xmlhttp.readyState==4 && xmlhttp.status==200) { document.getElementById("myDiv").innerHTML=xmlhttp.responseText; } }
    //配置参数 xmlhttp.open(
    "GET","/ajax/demo_get.asp",true);
    //提交请求 xmlhttp.send();

    下面是post类型,区别不大

    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","http://www.weisuyun.com/xiaochengxu.php",true);//GET改为POST
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");//这是添加http的头
    xmlhttp.send("name=Bill&sex=Gates");//send里面写参数

    最后是稍微整合一下的

    <html>
    <head>
    <script type="text/javascript">
    var xmlhttp;
    function loadXMLDoc(url,cfunc)
    {
    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=cfunc;
    xmlhttp.open("GET",url,true);
    xmlhttp.send();
    }
    function myFunction()
    {
    loadXMLDoc("/ajax/test1.txt",function()
      {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
        document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
        }
      });
    }
    </script>
    </head>
    <body>
    
    <div id="myDiv"><h2>Let AJAX change this text</h2></div>
    <button type="button" onclick="myFunction()">通过 AJAX 改变内容</button>
    
    </body>
    </html>
  • 相关阅读:
    2. linux下如何上传和下载文件
    (六)使用Docker镜像(下)
    (五)使用Docker镜像(上)
    1. chmod命令
    阿里P7/P8学习路线图——技术封神之路
    问题二:pip install python-igraph 报错,C core of igraph 没有安装。
    问题一:【Hive】explain command throw ClassCastException in 2.3.4
    (四)docker创建私人仓库
    P5024 保卫王国
    jzoj5980. 【WC2019模拟12.27】字符串游戏
  • 原文地址:https://www.cnblogs.com/zonglonglong/p/5915667.html
Copyright © 2011-2022 走看看