Ajax的含义,好处和两种传参方式

1.ajax的含义 Ajax全称“Async Javascript And XML”即:异步的javascript和XML。它是一种称谓,并不指代某项具体的技术,准确来说是一系列技术的集合.现在,所有的无刷新操作都被称为“Ajax”. 好处: 使用ajax避免了整页数据的刷新,也减少了请求等待的时间,提高了用户体验. 2.ajax的两种传参方式 (1)get方法

xhr=null;
try{
          
   
                xhr = new XMLHttpRequest();
            }catch(error){
          
   
                xhr = new ActiveXObject("Microsoft.XMLHTTP"); 
            }

xhr = new XMLHttpRequest()兼容IE8以上的(包括IE8) xhr = new ActiveXObject(“Microsoft.XMLHTTP”);兼容IE8以下的 绑定数据改变状态:

xhr.onreadystatechange = function(){
          
   
        if (this.readyState==4 && this.status==200){
          
   
            alert(this.responseText);
        }
    }

状态码 200代表请求成功。一般用于GET与POST请求 初始化:

xhr.open()

open里面第一个参数写你用的方法(get,post),第二个参数写请求的地址比如1.php,两个参数都要写引号,后面加true为异步执行,false为同步执行,默认为异步执行 发送:

xhr.send();

里面写需要发送的数据 (2)post方法 post方法和get方法大致相同,但post多了一个请求头

xhr=null;
try{
          
   
                xhr = new XMLHttpRequest();
            }catch(error){
          
   
                xhr = new ActiveXObject("Microsoft.XMLHTTP"); 
            }
            xhr.onreadystatechange = function(){
          
   
        if (this.readyState==4 && this.status==200){
          
   
            alert(this.responseText);
        }
    }
    xhr.open(post,,true)
    xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    xhr.send();

xhr.setRequestHeader(“Content-type”,“application/x-www-form-urlencoded”);请求头信息

经验分享 程序员 微信小程序 职场和发展