zoukankan      html  css  js  c++  java
  • 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)
                }
            })
  • 相关阅读:
    PKU1008
    PKU 1007
    PKU 3983
    PKU 1005
    PKU1004
    PKU 1003解题
    new.target
    debugger 关键字
    React 高阶组件
    CSS|规范
  • 原文地址:https://www.cnblogs.com/babilong/p/13653974.html
Copyright © 2011-2022 走看看