php - 根据泛型函数中的参数动态添加 ajax 处理程序

标签 php javascript jquery ajax

给你的脑筋急转弯。

我已经编写了这个通用的 JS 函数。我们的系统通过 AJAX 处理所有业务逻辑,我正在将代码整合到一个函数中,这样我就不必在代码库中更新或维护超过 300 万份相同的代码副本。

doRequest = function (data,msgBoxElem,origin) {
$.ajax({
  type: "POST",
  url: window.dispatcher_addr,
  data: data,
  dataType: "xml",
  contentType: "text/xml",
  processData: false,      
  success: function(xml) {
    $(msgBoxElem).html(xml.documentElement.getAttribute("msg"));
    location.reload();
  },
  error:function (xhr, ajaxOptions, thrownError){ if (thrownError != 200) {
    var Data = new Array();
    Data["errorcode"]       = xhr.status;
    Data["errortext"]       = thrownError;
    Data["calling_script"]  = origin;
    $.ajax({
      type: "POST",
      url: window.dispatcher_addr,
      data: CreatePayLoad('jslogerror',Data),
      dataType: "xml"        
    });
    }      
  }       

});

现在,某些页面具有在 $.ajax() 的成功、错误和完成处理程序中执行的特定逻辑。我想知道的是,如果可以将参数传递给 $.ajax(),然后它将执行自定义处理程序。

非常感谢。

米迪亚内。

最佳答案

您可以像这样向您的函数添加一个或多个回调参数,这将替换您的默认处理程序或在它们之外执行(您可以选择如何实现它)。下面是一个 fnSuccess 回调示例,它在您定义的默认行为之外执行:

doRequest = function (data,msgBoxElem,origin,fnSuccess) {
    $.ajax({
      type: "POST",
      url: window.dispatcher_addr,
      data: data,
      dataType: "xml",
      contentType: "text/xml",
      processData: false,      
      success: function(xml) {
        $(msgBoxElem).html(xml.documentElement.getAttribute("msg"));
        fnSuccess();
        location.reload();
      },
      error:function (xhr, ajaxOptions, thrownError){ if (thrownError != 200) {
        var Data = new Array();
        Data["errorcode"]       = xhr.status;
        Data["errortext"]       = thrownError;
        Data["calling_script"]  = origin;
        $.ajax({
          type: "POST",
          url: window.dispatcher_addr,
          data: CreatePayLoad('jslogerror',Data),
          dataType: "xml"        
        });
        }      
      }       
    }); 
}

或者,如果您希望执行调用者的代码而不是您的默认实现,您可以这样做:

doRequest = function (data,msgBoxElem,origin,fnSuccess) {
    $.ajax({
      type: "POST",
      url: window.dispatcher_addr,
      data: data,
      dataType: "xml",
      contentType: "text/xml",
      processData: false,      
      success: function(xml) {
          if (fnSuccess) {
              fnSuccess(xml);
          } else {
              $(msgBoxElem).html(xml.documentElement.getAttribute("msg"));
              location.reload();
          }
      },
      error:function (xhr, ajaxOptions, thrownError){ if (thrownError != 200) {
        var Data = new Array();
        Data["errorcode"]       = xhr.status;
        Data["errortext"]       = thrownError;
        Data["calling_script"]  = origin;
        $.ajax({
          type: "POST",
          url: window.dispatcher_addr,
          data: CreatePayLoad('jslogerror',Data),
          dataType: "xml"        
        });
        }      
      }       
    }); 
}

关于php - 根据泛型函数中的参数动态添加 ajax 处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9603467/

相关文章:

php - 最多 24 小时更新数据库

javascript - 在网络中重新着色图像

javascript - 如何通过仅将鼠标悬停在父级上来调节子级的样式?

javascript - jQuery/JavaScript 正则表达式匹配任意三个前字符后跟一个精确的字符串

javascript - jQuery : other links inside div element link

javascript确认向get/post添加参数

php - 更改复选框值并插入到数据库

jquery 属性后缺少 {

php - 弄清楚加载了哪些文件以及加载顺序

javascript - 与 Internet Explorer 的同步问题