javascript jQuery - 将函数数组传递给 ajax 成功回调
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9121011/
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
jQuery - pass array of functions to ajax success callback
提问by MJS
I'm fairly new with jQuery, and I'm trying to call two functions on successful ajax (since the documentation says as of 1.5 success callback can take an array of functions).
我对 jQuery 相当陌生,我试图在成功的 ajax 上调用两个函数(因为文档说从 1.5 开始,成功回调可以采用一组函数)。
If I do this, everything works fine:
如果我这样做,一切正常:
$.ajax({
url : sJSONFilePath,
dataType : 'json',
success : foo(data)
});
What do I need to do to pass in an array of functions? If I try the following, I get a "Uncaught TypeError: Cannot read property 'length' of undefined" error in the console:
我需要做什么来传递函数数组?如果我尝试以下操作,我会在控制台中收到“未捕获的类型错误:无法读取未定义的属性‘长度’”错误:
$.ajax({
url : sJSONFilePath,
dataType : 'json',
success : [foo(data), bar(data)]
});
I was unable to find any examples of this feature being used. Thanks in advance, and sorry if this is dumb.
我找不到任何使用此功能的示例。提前致谢,如果这很愚蠢,请见谅。
回答by Alnitak
There's no need for an array, you can use the deferred syntax that was also introduced in jQuery 1.5:
不需要数组,您可以使用 jQuery 1.5 中引入的延迟语法:
$.ajax(...).done(foo).done(bar);
This is generally cleaner and much more extensible than passing callback functions as parameters to $.ajax
and relatives.
这通常比将回调函数作为参数传递给$.ajax
和亲戚更干净,更可扩展。
From the $.ajax()
documentation:
The jqXHR objects returned by
$.ajax()
as of jQuery 1.5 implement the Promise interface, giving them all the properties, methods, and behavior of a Promise (see Deferred objectfor more information). For convenience and consistency with the callback names used by$.ajax()
, jqXHR also provides.error()
,.success()
, and.complete()
methods. These methods take a function argument that is called when the$.ajax()
request terminates, and the function receives the same arguments as the correspondingly-named$.ajax()
callback. This allows you to assign multiple callbacks on a single request, and even to assign callbacks after the request may have completed. (If the request is already complete, the callback is fired immediately.)
从
$.ajax()
jQuery 1.5 开始返回的 jqXHR 对象实现了 Promise 接口,为它们提供了 Promise 的所有属性、方法和行为(有关更多信息,请参阅延迟对象)。为了方便和一致性通过使用回调名称$.ajax()
,jqXHR还提供.error()
,.success()
和.complete()
方法。这些方法采用在$.ajax()
请求终止时调用的函数参数,该函数接收与相应命名的$.ajax()
callback相同的参数。这允许您在单个请求上分配多个回调,甚至在请求可能完成后分配回调. (如果请求已经完成,则立即触发回调。)
[but see the paragraph after, where it explains how .success
, .error
and .complete
are now deprecated and replaced with .done
, .fail
and .always
]
[但看后的一段,它解释了如何.success
,.error
而.complete
现在已过时,取而代之的.done
,.fail
并且.always
]
回答by T.J. Crowder
What you do is this:
你要做的是:
$.ajax({
url : sJSONFilePath,
dataType : 'json',
success : function(data) {
foo(data);
bar(data);
}
});
or this:
或这个:
$.ajax({
url : sJSONFilePath,
dataType : 'json',
success : [foo, bar]
});
Note that in the second one, I'm not callingfoo
and bar
, I'm listing the function references in an array (since as you say, as of 1.5 jQuery's ajax callback options allow that).
请注意,在第二个中,我没有调用foo
and bar
,而是在数组中列出函数引用(因为正如您所说,从 1.5 jQuery 的 ajax 回调选项开始,允许这样做)。
You've said that this works fine:
你说过这很好用:
$.ajax({
url : sJSONFilePath,
dataType : 'json',
success : foo(data)
});
but it doesn't. What that does is immediately callthe foo
function, and then assign the return value of that function to the success
property of the options you're passing to ajax
. Unless you're using foo
to build and return a function to use as the callback, that's not what you want to do.
但事实并非如此。它所做的是立即调用该foo
函数,然后将该函数的返回值分配给success
您传递给的选项的属性ajax
。除非您使用foo
构建并返回一个函数作为回调函数,否则这不是您想要做的。
It's important to understand the difference between callinga function and using a reference to it. If you have parens after the function name (with or without arguments in them), you're callingit. If you just have the name, you're referring to it. E.g.:
了解调用函数和使用对函数的引用之间的区别很重要。如果您在函数名称后有括号(其中包含或不包含参数),那么您就是在调用它。如果你只有名字,你指的是它。例如:
var f = foo(); // CALLs `foo` and assigns return value to `f`
var f = foo; // Assigns a reference to `foo` to `f`
回答by Jerome WAGNER
when writing
写作时
success : [foo(data), bar(data)]
you are in fact evaluating the foo and bar functions (probably with a null argument)
您实际上正在评估 foo 和 bar 函数(可能带有空参数)
you need to write
你需要写
success : [foo, bar]
回答by Wes Crow
Why don't you call one function that calls the other two:
为什么不调用一个函数来调用另外两个函数:
$.ajax({
url : sJSONFilePath,
dataType : 'json',
success : foo_bar(data)
});
function foo_bar(data)
{
foo(data);
bar(data);
{