Ajax page

let xhr;

if(XMLHttpRequest){

   xhr = new XMLHttpRequest;

}else{

    xhr = new ActiveXObject("Microsoft.XMLHTTP");

}

let dataStr = jsonToString (data); // data transmission in JSON format need to be converted into a format string to be written in js code.

// When the server initiates a request, the request includes get and post are two kinds of data that can be passed to the server

// GET request: after the data transfer is with the url of the open Remedies ⾯.

if(type.toUppercase()==''GET'){ 

    xhr.open(type,url+"?"+dataStr, async);

    xhr.send();

}else{

// the POST request: the data transfer parameters on the send Remedies.

     xhr.open(type , url , async );

    xhr.send(dataStr); //等于xhr.send("name='zs'&age='18'");

}

xhr.onreadystatechange = function(){

      Whether //log(xhr.readyState);// test status output

    if(xhr.readyState ==4 && ((xhr.sttus>=200 xhr.sttus<300) || xhr.status==304)){

            let data=xhr.responseText;

            success (data); // success callback

        }

  }

function jsonToString(obj){

   let arr=[ ];

   for(const key in obj){

      const value=obj[key];

      arr.push(`${key},${value}`);

     }

    return arr.join("&");

}

ajax({

    type:"GET";

    url:“https://getman.cn/mock/,,,”,

    data:{name:"zs", age:18},

    sucess(data){ console.log("daying ",data)}

});

getman.cn/mock/

Guess you like

Origin www.cnblogs.com/yt0817/p/12007196.html