jQuery Ajax 回调函数

可以使用回调函数来处理当Ajax申请返回值,也可以处理Ajax申请失败。

此种用法相当于事件驱动模式。

// Ajax Requests
// 2017.09.13 pickup $.Ajax.Query
(function($) {
  $.Ajax = {
    // done_cb: function(resp);
    // error_cb: function(xhr, status, error);
    Query: function(url, data, cb_done, cb_error, timeout) {
      $.ajax({
        url: url, data: null, method: 'post',
        success: cb_done, error: cb_done,
        timeout: timeout || 5000, dataType: 'json',
        //scriptCharset: 'utf-8',
        //contentType: "application/x-www-form-urlencoded; charset=utf-8",
      })
    }
  }
}) (jQuery);


// HOWTO call
$(function() {
  var cb_done = function(jsonResp) {
    console.log('Callback of cb_done() called', jsonResp);
  }
  var cb_error = function(xhr, status, error) {
    console.log('Callback of cb_error() called', xhr, status, error);
  };

  $.Request.Ajax('lite.php?do=devices', null, cb_done, cb_error);
});

results matching ""

    No results matching ""