English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

jQuery Version of AJAX Simplified Encapsulation Code

Experience Notes

正文
  /**
   * Ajax Encapsulation
   * url Address to send the request
   * data Data sent to the server, stored in an array, such as: {"date": new Date().getTime(), "state": 1}
   * During the development process, the application of AJAX should be very frequent, of course, jQuery's AJAX function is already very convenient, but the editor still wants to organize it a little, so that it can simplify the input parameters under different needs, and here is an example code:
   *    $(function(){
   * async Default value: true. By default, all requests are asynchronous. If you need to send a synchronous request, please set this option to false.
   * dataType Expected data type returned by the server, commonly used such as: xml, html, json, text
   * successfn Success Callback Function
   * errorfn Failure Callback Function
   */
  Note that synchronous requests will lock the browser, and users must wait for the request to complete before they can perform other operations.
    type Request method ("POST" or "GET"), default is "GET"63jQuery.syncAjax=function(url, data, async, type, dataType, successfn, errorfn) {
    async = (async==null || async=="" || typeof(async)=="undefined")? "true" : async;
    ; "post" : type;63; "json" : dataType;
    data = (data==null || data=="" || typeof(data)=="undefined")? {"date": new Date().getTime()} : data;
    $.ajax({
      type: type,
      async: async,
      data: data,
      url: url,
      dataType: dataType,
      success: function(d){
        successfn(d);
      },
      error: function(e){
        errorfn(e);
      }
    });
  });
  /**
   * Ajax Encapsulation
   * url Address to send the request
   * data Data sent to the server, stored in an array, such as: {"date": new Date().getTime(), "state": 1}
   * successfn Success Callback Function
   */
  jQuery.jsonAjax=function(url, data, successfn) {
    data = (data==null || data=="" || typeof(data)=="undefined")? {"date": new Date().getTime()} : data;
    $.ajax({
      type: "post",
      data: data,
      url: url,
      dataType: "json",
      success: function(d){
        successfn(d);
      }
    });
  });
  /**
   * Ajax Encapsulation
   * url Address to send the request
   * data Data sent to the server, stored in an array, such as: {"date": new Date().getTime(), "state": 1}
   * dataType Expected data type returned by the server, commonly used such as: xml, html, json, text
   * successfn Success Callback Function
   * errorfn Failure Callback Function
   */
  jQuery.jsonAjax2=function(url, data, successfn, errorfn) {
    data = (data==null || data=="" || typeof(data)=="undefined")? {"date": new Date().getTime()} : data;
    $.ajax({
      type: "post",
      data: data,
      url: url,
      dataType: "json",
      success: function(d){
        successfn(d);
      },
      error: function(e){
        errorfn(e);
      }
    });
  });
});

That's all for the content of this article, I hope it will be helpful to everyone's study, and I also hope everyone will support the Yelling Tutorial more.

Statement: The content of this article is from the Internet, the copyright belongs to the original author, the content is contributed and uploaded by Internet users spontaneously, this website does not own the copyright, has not been edited by humans, and does not bear relevant legal liability. If you find content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report violations, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)

You May Also Like