typescript 在 ajax .done 中为函数提供了哪些参数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13141028/
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-10-21 02:46:01  来源:igfitidea点击:

What arguments are supplied to the function inside an ajax .done?

javascriptajaxjquerytypescript

提问by Alnitak

I have the following:

我有以下几点:

    $.ajax(link.href,
    {
        cache: false,
        dataType: 'html'
    })
        .done(onDialogDone)
        .fail(onDialogFail);

This works fine and onDialogDone is called. However what arguments should I expect to see supplied to the onDialogDone and what should I expect to see for onDialogFail.

这工作正常,并且 onDialogDone 被调用。但是,我应该期望看到提供给 onDialogDone 的参数以及我应该期望看到 onDialogFail 的参数。

The reason I am asking is because I use typescript and I want to supply the correct arguments when I define my onDialogDone and onDialogFail.

我问的原因是因为我使用 typescript 并且我想在定义 onDialogDone 和 onDialogFail 时提供正确的参数。

回答by Alnitak

The arguments for .done()and .fail()are the same as the arguments for the corresponding success:and error:parameters for the $.ajax()function, namely:

为参数.done().fail()是相同的参数用于相应success:error:参数为$.ajax()函数,即:

.done( function(data, textStatus, jqXHR) { ... } );

and

.fail( function(jqXHR, textStatus, errorThrown) { ... } );

For the purposes of typescript, textStatusand errorThrownare strings, jqXHRis an Object, and datadepends on what the remote server sends you.

出于打字稿的目的,textStatus并且errorThrown是字符串,jqXHRObject, 并且data取决于远程服务器向您发送的内容。

回答by Fenton

The three parameters passed to the donehandler are:

传递给done处理程序的三个参数是:

data, textStatus, jqXHR

You can read more here: http://api.jquery.com/jQuery.ajax/

您可以在此处阅读更多信息:http: //api.jquery.com/jQuery.ajax/

  1. datais the response message
  2. textStatuswill always be success in the donefunction
  3. jqXHRis the raw XMLHttpRequest
  1. data是响应消息
  2. textStatusdone功能上永远是成功的
  3. jqXHR是原始的 XMLHttpRequest

回答by thedev

Check thisout:

看看这个

Methods (part of jqXHR and Deferred implementations, shown here for clarity only)

方法(jqXHR 和 Deferred 实现的一部分,这里显示只是为了清楚起见)

 .ajax().always(function(a, textStatus, b){});

Replaces method .complete() which was deprecated in jQuery 1.8. In response to successful transaction, arguments are same as .done() (ie. a = data, b = jqXHR) and for failed transactions the arguments are same as .fail() (ie. a = jqXHR, b = errorThrown). This is an alternative construct for the complete callback function above. Refer to deferred.always() for implementation details.

替换 jQuery 1.8 中弃用的方法 .complete()。对于成功的交易,参数与 .done() 相同(即 a = data, b = jqXHR),对于失败的交易,参数与 .fail() 相同(即 a = jqXHR, b = errorThrown)。这是上述完整回调函数的替代构造。有关实现细节,请参阅 deferred.always()。

    .ajax().done(function(data, textStatus, jqXHR){});

Replaces method .success() which was deprecated in jQuery 1.8. This is an alternative construct for the success callback function above. Refer to deferred.done() for implementation details.

替换在 jQuery 1.8 中已弃用的方法 .success()。这是上述成功回调函数的替代构造。有关实现细节,请参阅 deferred.done()。

    .ajax().fail(function(jqXHR, textStatus, errorThrown){});

Replaces method .error() which was deprecated in jQuery 1.8. This is an alternative construct for the complete callback function above. Refer to deferred.fail() for implementation details.

替换在 jQuery 1.8 中不推荐使用的方法 .error()。这是上述完整回调函数的替代构造。有关实现细节,请参阅 deferred.fail()。

    .ajax().then(function(data, textStatus, jqXHR){}, function(jqXHR, textStatus, errorThrown){});

Incorporates the functionality of .done() and .fail() methods. Refer to deferred.then() for implementation details.

结合了 .done() 和 .fail() 方法的功能。有关实现细节,请参阅 deferred.then()。

    .ajax().pipe(function(data, textStatus, jqXHR){}, function(jqXHR, textStatus, errorThrown){});

Incorporates the functionality of .done() and .fail() methods, allowing the underlying Promise to be manipulated. Refer to deferred.pipe() for implementation details.

结合了 .done() 和 .fail() 方法的功能,允许操作底层的 Promise。有关实现细节,请参阅 deferred.pipe()。