zoukankan      html  css  js  c++  java
  • 手写原生态ajax封装代码

    function createAjaxObj(){
     var httprequest=false
     if (window.XMLHttpRequest){ // if Mozilla, Safari etc
      httprequest=new XMLHttpRequest()
      if (httprequest.overrideMimeType)
       httprequest.overrideMimeType('text/xml');
     }
     else if (window.ActiveXObject){ // if IE
      try 
      {
       httprequest=new ActiveXObject("Msxml2.XMLHTTP");
      } 
      catch (e)
      {
       try
       {
        httprequest=new ActiveXObject("Microsoft.XMLHTTP");
       }
       catch (e){}
      }
     }
     return httprequest;
    }
    
     
     
     
     
     
     
     
     
     
     
     
    postStr+="&lan="+document.getElementById("lanVersion").value;
     
    // alert(postStr);
     //实例化Ajax
     var ajax = createAjaxObj(); 
     //通过Post方式打开连接
     ajax.open("POST", url, true); 
     //定义传输的文件HTTP头信息,这一行不能少
     ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");  
     //发送POST数据
     ajax.send(postStr);
     ajax.onreadystatechange = function() { 
      if (ajax.readyState == 4 && ajax.status == 200) { 
    //        document.getElementById("content").value=""
    //        document.getElementById("bntSendProEmail").disabled=false;
            var content=ajax.responseText;
            alert(content); 
      } 
      else if(ajax.readyState != 1 && ajax.readyState != 2 && ajax.readyState != 3&& ajax.readyState != 0)
      {
    //   document.getElementById("bntSendProEmail").disabled=true;
       alert("评论发表失败,评论内容含有非法信息!"); 
      }
     }
  • 相关阅读:
    微服务实战(三):深入微服务架构的进程间通信
    微服务实战(二):使用API Gateway
    微服务实战(一):微服务架构的优势与不足
    函数声明与函数表达式
    CSS样式优先级
    iframe框架及优缺点
    JS事件流模型
    JS事件冒泡及阻止
    浏览器重绘与回流
    浏览器渲染与内核
  • 原文地址:https://www.cnblogs.com/lcuzhanglei/p/2624331.html
Copyright © 2011-2022 走看看