我在 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/