简体   繁体   English

作法:以Ajax get阵列为参数的回呼函式

[英]How to: callback function that takes ajax get array as a parameter

How would I go about writing a function that takes a function as a parameter and runs the callback function with an array pulled from the server as its parameter. 我将如何编写一个将一个函数作为参数并以从服务器中拉出的数组作为其参数运行回调函数的函数。 So if I ran something like: 因此,如果我运行类似的内容:

getArray(display)

getArray would retrieve an array from the server, then display would write that array into the html. getArray将从服务器中检索一个数组,然后显示会将数组写入html。

Like so: 像这样:

function getArray(callback) {
    $.ajax({
        ..//options
        success: function(data) {
            callback(data); 
        }
    });
}

function display(array) {
    console.log(array);
}

//call it!
getArray(display);

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

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