【发布时间】:2019-12-01 23:09:51
【问题描述】:
我正在使用与此问题相同的解决方案。但是,我想在 when() 方法之后加载另一个 JSON 文件。所以在when() 中我得到一个JSON 数据,然后基于此,在then() 方法中,我想加载另一个JSON。
这是我的代码:
var myJSON;
var myJSON2;
function _parseJSON() {
// return here, no need returning exactly what you would expect anyhow
return $.getJSON(settings.json_src);
}
function _parseJSON2(term) {
return $.getJSON("http://website.com/" + term ".json");
}
// because you returned, you can chain everything nicely together
$.when( _parseJSON() )
// don't forget the argument here
.then(function( data ) {
myJSON = data;
myJSON2 = _parseJSON2(data.value);
})
// and these will only run after the myJSON got set
.then( _cacheOptions )
.then( _cacheDom )
.then( _events )
.then( _render );
myJSON2 = _parseJSON2(data.value); 行必须在加载 data 时运行,并且必须将 myJSON2 保存为变量以供稍后在代码中使用。
这更像是另一个when().then() 是必需的。
知道如何让它工作吗?
提前致谢。
【问题讨论】:
标签: javascript jquery json jsonp getjson