How can I pass parameters to a jQuery $.getJSON callback method?

前端 未结 5 2138
走了就别回头了
走了就别回头了 2020-12-29 23:50

I\'m trying to use jQuery to call some custom API via Ajax/$.getJSON.

I\'m trying to pass a custom value into the Ajax callback method, but that value

5条回答
  •  醉酒成梦
    2020-12-30 00:36

    You don't need to pass it in, just reference the variable you already have, like this:

    var locationType = 3;
    var url = 'blah blah blah' + '&locationType=' + locationType;
    $("#loading_status").show();
    $.getJSON(url, null, function(results) {
        searchResults(results, locationType)
    });
    

    Also there's no need to pass null if you don't have a data object, it's an optional parameter and jQuery checks if the second param is a function or not, so you can just do this:

    $.getJSON(url, function(results) {
        searchResults(results, locationType)
    });
    

提交回复
热议问题