jquery - 获取范围之外的 JSON 数据

标签 jquery json function callback

我在 JSON 中有此数据:

{"minmax":["0.01","67.00"]}

我想使用 jQuery 来获取它,这就是我正在做的:

$.getJSON("../../dados/opcoesMinMax.json", function(data) {
    var getMinMax = data;
});

// Need to use data here, out of the scope

我也尝试使用回调来做到这一点,这样做:

function getMinMax(callback) {
    $.getJSON("../../dados/opcoesMinMax.json", function(data) {
        callback(JSON.stringify(data));
    });
}

// Need to use data here, out of the scope

即使使用回调,我也无法恢复数据。 console.log(getMinMax); 返回该函数。 和 console.log(getMinMax()); 返回未定义并表示回调不是函数

最佳答案

您使用的第二个模式是有效的,并且可以工作。问题是因为在调用 getMinMax() 时,您需要提供回调函数作为参数。这就是您当前看到“回调不是函数”错误的原因。试试这个:

function getMinMax(callback) {
  $.getJSON("../../dados/opcoesMinMax.json", callback);
}

getMinMax(data => {
  // this is the callback function. Work with the data here...
  console.log(data);
});

关于jquery - 获取范围之外的 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66986967/

相关文章:

javascript - 为集团文本着色,使其看起来像法国国旗

javascript - 设置 JQGrid 以便所有行都可编辑?

javascript - JSON pretty-print 并突出显示

objective-c - 无法将 JSON 放入 NSDictionary

C++ 模板没有匹配的函数调用

python - Python 中的全局变量

javascript - JQGrid 自定义格式化程序 - 无需格式化程序即可编辑/保存/取消 : 'action'

jquery - 获取第 n 个 :child of specific element type

python - MemoryError 使用 json.dumps()

c - 在c中制作一个将小写字符串转换为大写字符串的函数