Javascript 理解 jQuery 的 jqXHR

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7638847/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:00:49  来源:igfitidea点击:

Understanding jQuery's jqXHR

javascriptjquery

提问by P.Brian.Mackey

I have a set of $.get()requests that I need to refactor to include a failure callback. The requests are of the form

我有一组$.get()需要重构以包含失败回调的请求。请求的形式是

$.get(url,
      {},
      function(data) {//success, do work
      //work
      },'json');//or 'html'

According to the jQuery API, I simply add a jqHXR object. So in my case, I believe I should do

根据 jQuery API,我只是添加了一个 jqHXR 对象。所以就我而言,我相信我应该这样做

var jqxhr =  $.get(url,
          {},
          function(data) {//success, do work
          //work
          },'json').error(function() { alert("error"); });//or 'html'

I don't understand the reason for the second success callback in the example. I suppose it could be there for setting up a callback chain. I want error to execute in error, and success to execute on success. So, is this correct?

我不明白示例中第二次成功回调的原因。我想它可以用于设置回调链。我希望错误执行错误,成功执行成功。那么,这是正确的吗?

回答by nrabinowitz

I think the second success callback in the example is just there to illustrate that, using this syntax, you can have multiple handlers for success, error, and completeevents. In the standard jQuery .ajax()method, you can only assign a single handler to each of these events. I can't think offhand of an example that would requiremultiple handlers, but it does seem a little clearer and more like the standard jQuery idiom to use

我觉得在这个例子中的第二个成功回调只是为了说明,使用此语法,你可以有多个处理程序successerrorcomplete事件。在标准的 jQuery.ajax()方法中,您只能为这些事件中的每一个分配一个处理程序。我想不出一个需要多个处理程序的例子,但它看起来更清晰,更像标准的 jQuery 习惯用法

$.get('my_url.php')
    .success(handlerOne)
    .success(handlerTwo);

instead of

代替

$.get('my_url.php', function(data, textStatus, jqXHR) {
    handlerOne(data, textStatus, jqXHR);
    handlerTwo(data, textStatus, jqXHR);
});

In your case, though, it might be easier and cleaner to just convert your $.get()statements to $.ajax(). The $.ajax()syntax is probably more familiar to most jQuery programmers, and since you don't need the special functionality (multiple handlers, post-request handler assignment) available in the other syntax, there's no reason not to just use $.ajax():

但是,在您的情况下,将您的$.get()语句转换为$.ajax(). $.ajax()大多数 jQuery 程序员可能更熟悉该语法,并且由于您不需要其他语法中可用的特殊功能(多个处理程序、请求后处理程序分配),因此没有理由不使用$.ajax()

$.ajax({
    url: url,
    success: function(data) {
        // success, do work
    },
    error: function(data) {
        // error, handle failure
    },
    dataType:'json'
});

回答by Steve Tauber

This page has a lot of documentation on how jqXHR (jQuery XHR) differs from XHR (XMLHttpRequest).

这个页面有很多关于 jqXHR (jQuery XHR) 与 XHR (XMLHttpRequest) 区别的文档。

http://api.jquery.com/jQuery.ajax/#jqXHR

http://api.jquery.com/jQuery.ajax/#jqXHR

回答by Kevin B

The code you are using should be fine. The second success is just another place where you can define a success method. Some people use the jqxhrsuccess instead of the one passed into $.get(), and others use the deferred object's done handler to do the same.

您使用的代码应该没问题。第二次成功只是您可以定义成功方法的另一个地方。有些人使用jqxhr成功而不是传入的$.get(),而其他人使用延迟对象的完成处理程序来做同样的事情。