【问题标题】:Zepto's $.post does not have error handlerZepto 的 $.post 没有错误处理程序
【发布时间】:2016-04-05 05:51:38
【问题描述】:

我在我的应用程序中使用Zepto 作为jQuery 的替代品,当我意识到$.ajax 有一个错误处理程序时,我正在处理一项任务,但其他方法如$.post$.get没有。

这可能是什么原因?

函数签名

$.post(url, [data], function(data, status, xhr){ ... }, [dataType])

$.get(url, [data], [function(data, status, xhr){ ... }], [dataType])

参考文献

  1. $.ajax
  2. $.post
  3. $.get

【问题讨论】:

    标签: javascript jquery ajax xmlhttprequest zepto


    【解决方案1】:

    根据您关于$.get$.post 的问题。是的,source code on github 回答说这个方法没有错误处理程序,但是您可以在$.ajaxSettings 中添加常见错误处理程序

    但是而不是 $.ajax 与回调更好地使用 Zepto deferred API。您必须包含it manually

    它提供了$.Deferred promises API。取决于“回调”模块。 包含时,$.ajax() 支持用于链接回调的承诺接口。

    使用 deferred,您可以在 deferred/promise 链中捕获错误:

    $.post(/*any options*/).done(/*success handler*/).fail(/*error handler*/)
    

    $.post().then(function() {
       // success code here
    }, function() {
       // error code here
    });
    

    【讨论】:

    • 这是一个很好的答案,但正如我所说的$.ajax 有错误处理程序,我已经更新了我的代码以使用它。问题是为什么其余方法中缺少错误处理程序。
    【解决方案2】:

    这不是错误,它只是 Zepto 模拟的 jQuery API 的设计方式(参见例如 https://api.jquery.com/jquery.get/)。

    如果您需要回调,请使用 $.ajax、全局回调或承诺。

    【讨论】:

    • 遗漏了一点 - jQuery 返回 jqXHR(它有错误方法),Zepto 原生 xhr。所以在 Zepto 设计中确实错过了错误处理程序。
    【解决方案3】:

    在我看来,错误处理程序不可用,因为它会增加混乱。
    当前功能说明:

    $.get(url, [data], [function(data, status, xhr){ ... }], [dataType])

    添加错误处理程序时,参数将转换为:

    $.get(url, [data], [function(data, status, xhr){ ... }], [function(data, status, xhr){ ... }], [dataType])

    在附加错误处理程序参数的情况下,在进行 ajax 调用时很难理解您的意思是什么处理程序:

    $.get('http://example.com', {query: 1}, function(result) {
        //Handle the request
    });
    

    在这种情况下,处理程序是针对错误还是成功?这很难理解。当然,您可以在参数中添加一些额外的nulls,但这不是一个干净的解决方案并且会增加混乱。

    $.ajax 有一个错误处理程序,因为它接受作为 JavaScript 对象的选项。如果您将错误处理程序函数指定为选项对象的属性,它不会产生任何问题。

    解决办法:
    只需使用承诺方法:

    var xhr = $.post(...);
    xhr.done(function(data, status, xhr){ 
      //Handle when success
    }).fail(function(xhr, errorType, error){
      //Handle when an error occurred.
    }).always(function(){ 
      //A handler executed always, on success or error
      //Use this to hide the loading image for example
    })
    

    调用时,ajax 调用函数将返回一个 promise 对象。附加到您的成功(使用done() 方法)和错误(using fail() 方法)处理程序的承诺。
    在任何情况下都会执行always()(在执行done()fail() 处理程序之后)。完成与请求相关的任何工作很有用,例如隐藏加载图像。

    【讨论】:

      【解决方案4】:

      @Pinal 答案的扩展,我相信 parseArguments 函数正在产生问题:

      原始码

      获取

      $.get = function(/* url, data, success, dataType */){
        return $.ajax(parseArguments.apply(null, arguments))
      }
      

      解析参数

      function parseArguments(url, data, success, dataType) {
        if ($.isFunction(data)) dataType = success, success = data, data = undefined
        if (!$.isFunction(success)) dataType = success, success = undefined
        return {
          url: url,
          data: data,
          success: success,
          dataType: dataType
        }
      }
      

      可能的解决方案

      function parseArguments(url, data, success, error, dataType) {
        if ($.isFunction(data)) dataType = success, success = data, data = undefined
        if (!$.isFunction(success)) dataType = success, success = undefined
        if (!$.isFunction(error)) dataType = error, error = undefined
        return {
          url: url,
          data: data,
          success: success,
          error:error,
          dataType: dataType
        }
      }
      

      但是是的,这可能是一个通用函数,更改它可能会产生重大影响。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-07
        • 2019-01-25
        • 2016-08-18
        • 1970-01-01
        • 1970-01-01
        • 2014-05-02
        • 2014-03-18
        相关资源
        最近更新 更多