JS and ajax in a comprehensive package jsonp

Ajax is an important means of front and rear ends data exchange
 
Ajax is a set of string technology:
 
    1.JavaScript, by the user or other related events to capture interactions with the browser
 
    It sends a request to the server without 2.XMLHttpRequest object, without interrupting other browser tasks through the object;
 
    3. The files on the server, save the text data to XML, HTML or JSON format;
 
    4. Other JavaScript, to interpret the data (such as PHP MySQL data obtained from) from the server and renders it onto the page.
 
ajax contains many features, advantages and disadvantages are also obvious. The main advantage of the following points:
 
    1. do not need to plug-in support (usually a browser and JavaScript can be enabled by default);
 
    2. The user experience is very good (not refresh the page to get the data can be updated);
 
    3. To enhance the performance of Web applications (sent to do on-demand, data transfer is not necessary in terms of the overall submitted);
 
    4. reduce the burden on the server and the bandwidth (the transfer of some of the operations of the server to the client);
 
ajax deficiencies by the following:
 
    1. The lack of different versions of the browser support for XMLHttpRequest objects degrees (such as before IE5);
 
    2. The forward and backward functions are destroyed (because Ajax is always the current page, the page will not be recorded before and after);
 
    3. Support of the search engine is not enough (because search engine crawlers can not understand the contents of the data changes caused by JS);

JSONP: html browser is the use of labels (such as: a, img, link, script, etc.) do not cross-domain request limits case, use the script to load url cross-domain interface.

NOTE: When the data present on the server requests: cross-domain occur;

Address Type: https://sp0.baidu.com/5a1Fazu8AA54nxGko9WTAnF6hhy/su?wd=&cb=liyang

function ajax(options){

  var {type, url, success, error, data} = options; // deconstruction mode to receive incoming value

  type = type || "get"; // request type is determined, and the default is get

  data = data || {}; // data determines whether data is empty, empty object set is empty

  var str = ""; for splicing // address

  for (var i in data) {// data traversing data, and stitching in accordance with the form of an http address

    str +=`${i}=${data[i]}&`;

  }

  if(type == "get" || type =="jsonp"){

    var d = new Date();

    url = url + + str + "_init_" + d.getTime () "?"; // stitching full address, add d.getTime () set the number of milliseconds to solve the problem browser cache

  }

  if (type == "jsonp") {// jsonp to solve the problem can not be cross-domain ajax

    var script = document.createElement("script");

    script.src = url;

    document.body.appendChild(script);

    window[data[data.column]] == function(res){

      success && success (res); return success (res) // When successful, and the error is set to air

      error = null;

    };

    setTimeout (function (res) {// Write Add

      error && error(res);//同success

      success = null;

    },2000);

  } Else if (type == "get" || type == "post") {// get when the type or types of post, construct ajax

    var xhr = new XMLHttpRequest();//1

    xhr.open(type,url,true);//2

    if(xhr.onreadystatechange == 4 && xhr.status == 200){//3.ajax成功
      success && success(xhr.responseText);
    }else if(xhr.onreadystatechange == 4 && xhr.status != 200){//ajax失败
      error && error(xhr.status);
    }
    // ajax method for sending the judge get and Post
    if(type == "get"){
      xhr.send();
    }else if(type == "post"){
      xhr.setRequestHeader ( "Content-Type", "application / x-www-form-urlencoded"); when added before the transmission // type of post
      xhr.send(str);
    }

  }

}

 

Guess you like

Origin www.cnblogs.com/nongfusanquan/p/11412556.html
Recommended