简体   繁体   中英

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.

Like so:

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

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

//call it!
getArray(display);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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