简体   繁体   English

jQuery-将函数数组传递给Ajax成功回调

[英]jQuery - pass array of functions to ajax success callback

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: 如果尝试以下操作,则会在控制台中收到“未捕获的TypeError:无法读取未定义的属性'length'的信息”错误:

$.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. 在此先感谢您,如果这很愚蠢,请多谢。

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 : $.ajax()文档中

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 object for more information). $.ajax()从jQuery 1.5开始由$.ajax()返回的jqXHR对象实现Promise接口,为它们提供Promise的所有属性,方法和行为(有关更多信息,请参见Deferred对象 )。 For convenience and consistency with the callback names used by $.ajax() , jqXHR also provides .error() , .success() , and .complete() methods. 为了方便和与$.ajax()使用的回调名称保持一致,jqXHR还提供了.error() .success().complete()方法。 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 . 这些方法采用一个函数参数,该参数在$.ajax()请求终止时被调用,并且该函数接收与相应名称的 $.ajax() 回调 相同的参数 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.) (如果请求已完成,则立即触发回调。)

[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 ]

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 calling foo 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). 请注意,在第二篇文章中,我没有调用 foobar ,而是在数组中列出了函数引用(正如您所说的那样,自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 call the foo function, and then assign the return value of that function to the success property of the options you're passing to ajax . 这样做是立即调用 foo函数,然后将该函数的返回值分配给要传递给ajax的选项的success属性。 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来构建并返回用作回调的函数,否则这不是您想要的。

It's important to understand the difference between calling a function and using a reference to it. 重要的是要了解调用函数和使用对函数的引用之间的区别。 If you have parens after the function name (with or without arguments in them), you're calling it. 如果在函数名称后面有括号(在函数名称中有或没有参数),则在调用它。 If you just have the name, you're referring to it. 如果只是名字,就是指它。 Eg: 例如:

var f = foo(); // CALLs `foo` and assigns return value to `f`
var f = foo;   // Assigns a reference to `foo` to `f`

when writing 写作时

success : [foo(data), bar(data)]

you are in fact evaluating the foo and bar functions (probably with a null argument) 您实际上正在评估foo和bar函数(可能带有null参数)

you need to write 你需要写

success : [foo, bar]

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);
{

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM