ajax创建过程?

一、创建过程

1.创建XMLHttpRequest对象,异步请求对象

2.创建一个新的htpp请求,并指定请求的方法、url、验证信息

3.发送请求

4.接收数据

二、Ajax封装

function ajax(options){
            var xhr;
            if(window.XMLHttpRequest){
                xhr=new XMLHttpRequest();
            }else{
                xhr=new ActiveXObject("Microsoft.XMLHTTP");
            }
            if(options.type==='get'){
                xhr.open(options.type,options.url+"?"+params,options.async);
                xhr.send(null);
            }
            if(options.type==="post"){
                xhr.open(options.type,options.url,options.async);
                xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
                xhr.send(params)
            }
            xhr.onreadystatechange=function(){
                if(xhr.readyState==4&&xhr.status==200){
                    options.success(xhr.responseText);
                }
            }
            function farmsParams(data){
                var arr=[];
                for(let d in data){
                    arr.push(p+"="+arr[p])
                }
                return arr.join("&&")
            }

        }

示例:

  ajax({
            type:"get",
            url:"",
            async:true,
            success:function(data){
                console.log(data)
            }
        })
原文地址:https://www.cnblogs.com/babilong/p/13653974.html