zoukankan      html  css  js  c++  java
  • AJAX

    主要是要掌握它的几个对象,还有一些固定写法

    固定写法:

    function ajaxSubmit()
     {
      if(window.ActiveXObject) // IE浏览器
      {
       xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
      }
      else if(window.XMLHttpRequest) //除IE外的其他浏览器实现
      {
       xmlHttpRequest = new XMLHttpRequest();
      }
      
      if(null != xmlHttpRequest)
      {
       var v1 = document.getElementById("value1ID").value;
       var v2 = document.getElementById("value2ID").value;
       
       xmlHttpRequest.open("POST", "AjaxServlet", true);
       //关联好ajax的回调函数
       xmlHttpRequest.onreadystatechange = ajaxCallback;
       
       //真正向服务器端发送数据
       // 使用post方式提交,必须要加上如下一行,Content-Type记不住怎么办?在myeclipse中打form标签的提示即可
       xmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
       xmlHttpRequest.send("v1=" + v1 + "&v2=" + v2);
      }
     }

    回调函数写法:

    function ajaxCallback()
     {
      if(xmlHttpRequest.readyState == 4)
      {
       if(xmlHttpRequest.status == 200)
       {
        var responseText = xmlHttpRequest.responseText;
        
        document.getElementById("div1").innerHTML = responseText;
       }
      }
     }

    上面的是用POST方式提交的,如果用GET方式呢?

    有三处不一样:

    xmlHttpRequest.open("GET", "AjaxServlet", true);//url后面要跟上请求参数,方法名是GET

     xmlHttpRequest.send(null);

  • 相关阅读:
    类和接口的区别
    ref 和out的区别
    重载 重写 多态区别
    UML种类与类的关系
    Window.open() 全攻略
    C#委托
    RT Diagnostics Routines
    tar.bz2 解压命令。
    教你10招最有效防电脑辐射方法
    预处理,编译, 优化, 汇编, 链接
  • 原文地址:https://www.cnblogs.com/qiuh/p/3039827.html
Copyright © 2011-2022 走看看