首页 > 解决方案 > Creating a function to use in getJSON callback

问题描述

I want a function that I can use in different getJSON calls, but I get a data not defined error

jQuery(document).ready(function($) {
function populateCalendarMonth(data)
{
    console.log(data);
}
var d = new Date().toJSON().slice(0, 10);
var args = {
        "action": "church_admin",
        "method": "calendar-render",
        "date": d
    };

$.getJSON(ajaxurl,args,populateCalendarMonth(data))

});

Doesn't work, but

$.getJSON(ajaxurl,args,function(data){console.log(data)});

Shows my JSON data beautifully in the console.

How do I create a function that can be used again? (eventually the calendar will be populated on page load with this month and other months on clicking next and previous!)

标签: jqueryfunctiongetjson

解决方案


You don't need to add parameter to your call, as that's passed by the callback by itself.

$.getJSON(ajaxurl, args, populateCalendarMonth)

推荐阅读