• [代码]JS原生Ajax,GET和POST


    javascript/js的ajax的GET请求:

    <script type="text/javascript"> 
    /* 创建 XMLHttpRequest 对象 */ 
    var xmlHttp; 
    function GetXmlHttpObject(){ 
      if (window.XMLHttpRequest){ 
        // code for IE7+, Firefox, Chrome, Opera, Safari 
        xmlhttp=new XMLHttpRequest(); 
      }else{// code for IE6, IE5 
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
      } 
      return xmlhttp; 
    } 
    // -----------ajax方法-----------// 
    function getLabelsGet(){ 
      xmlHttp=GetXmlHttpObject(); 
      if (xmlHttp==null){ 
        alert('您的浏览器不支持AJAX!'); 
        return; 
      } 
      var id = document.getElementById('id').value; 
      var url="http://www.Leefrom.com?id="+id+"&t/"+Math.random(); 
      xmlHttp.open("GET",url,true); 
      xmlHttp.onreadystatechange=favorOK;//发送事件后,收到信息了调用函数 
      xmlHttp.send(); 
    }
    function getOkGet(){ 
      if(xmlHttp.readyState==1||xmlHttp.readyState==2||xmlHttp.readyState==3){ 
        // 本地提示:加载中 
      } 
      if (xmlHttp.readyState==4 && xmlHttp.status==200){ 
        var d= xmlHttp.responseText; 
        // 处理返回结果 
      } 
    } 
    </script>

    javascript/js的ajax的POST请求:

    <script type="text/javascript"> 
    /* 创建 XMLHttpRequest 对象 */ 
    var xmlHttp; 
    function GetXmlHttpObject(){ 
        if (window.XMLHttpRequest){ 
          // code for IE7+, Firefox, Chrome, Opera, Safari 
          xmlhttp=new XMLHttpRequest(); 
        }else{// code for IE6, IE5 
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
        } 
        return xmlhttp; 
    } 
    // -----------ajax方法-----------// 
    function getLabelsPost(){ 
        xmlHttp=GetXmlHttpObject(); 
        if (xmlHttp==null){ 
            alert('您的浏览器不支持AJAX!'); 
            return; 
        } 
        var url="http://www.lifefrom.com/t/"+Math.random(); 
        xmlhttp.open("POST",url,true); 
        xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); 
        xmlhttp.send(); 
        xmlHttp.onreadystatechange=getLabelsOK;//发送事件后,收到信息了调用函数 
    } 
    function getOkPost(){ 
        if(xmlHttp.readyState==1||xmlHttp.readyState==2||xmlHttp.readyState==3){ 
            // 本地提示:加载中/处理中 
                                                      
        } 
        if (xmlHttp.readyState==4 && xmlHttp.status==200){ 
            var d=xmlHttp.responseText; // 返回值 
            // 处理返回值 
        } 
    } 
    </script> 

    注意:XMLHttpRequest 是 AJAX 的基础,在创建 XMLHttpRequest 对象时,必须与你写的ajax方法在同一个‘<script></script>’标签中!否则ajax请求会出错,并无法返回数据。 javascript/js的ajax的POST/GET请求。

  • 相关阅读:
    [算法] 选择排序和插入排序
    [Machine-Learning] 机器学习中的几个度量指标
    [SDN] What is SDN?
    SpringBoot注解大全*(转发:http://www.cnblogs.com/ldy-blogs/p/8550406.html)
    java定时器的使用(Timer)(转发:https://blog.csdn.net/ecjtuxuan/article/details/2093757)
    Quartz使用总结(转发:http://www.cnblogs.com/drift-ice/p/3817269.html)
    Spring IOC/DI和AOP原理(转发:https://www.cnblogs.com/techroad4ca/p/5770073.html)
    Java设计模式之《单例模式》及应用场景(转发:http://www.cnblogs.com/V1haoge/p/6510196.html)
    JDBC注册驱动程序的三种方式
    数据库三范式的简单理解
  • 原文地址:https://www.cnblogs.com/xiaocaocao/p/4882309.html
Copyright © 2020-2023  润新知