如何在其他 JSON 加载后 运行 一个 JSON
How to run a JSON after other JSON is loaded
我正在使用与这个问题相同的解决方案。但是,我想在 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 );
加载 data
时,行 myJSON2 = _parseJSON2(data.value);
必须是 运行,并且 myJSON2
必须保存为变量,以便稍后在代码中使用。
它更像是需要另一个 when().then()
。
知道如何让它工作吗?
提前致谢。
您的 $.when
实际上是多余的,但这并没有真正引起问题
_parseJSON2(data.value)
你缺少的是 return 它作为一个承诺,同时将它的 response 分配给 myJSON2
你可以这样做:
function _parseJSON() {
return $.getJSON(settings.json_src)
}
/**
* Receives data from _parseJSON() and asssigns to myJSON variable
* Assigns new response to myJSON2
*/
function _parseJSON2(data) {
myJSON = data;
const term = myJSON.value
return $.getJSON("http://website.com/" + term ".json")
// this is the then() you were missing
.then(function(data){
myJSON2 = data;
// optionally return myJSON2 to next `then()`
});
}
// $.when not really needed
_parseJSON()
.then( _parseJSON2 )
.then( _cacheOptions )
.then( _cacheDom )
.then( _events )
.then( _render );
根据mozila.org
如果使用fetch函数就不用jQuery
fetch("http://website.com/" + term ".json")
.then(function(response) {
return response.json();
})
.then(function(myJson) {
console.log(JSON.stringify(myJson));
});
我正在使用与这个问题相同的解决方案。但是,我想在 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 );
加载 data
时,行 myJSON2 = _parseJSON2(data.value);
必须是 运行,并且 myJSON2
必须保存为变量,以便稍后在代码中使用。
它更像是需要另一个 when().then()
。
知道如何让它工作吗?
提前致谢。
您的 $.when
实际上是多余的,但这并没有真正引起问题
_parseJSON2(data.value)
你缺少的是 return 它作为一个承诺,同时将它的 response 分配给 myJSON2
你可以这样做:
function _parseJSON() {
return $.getJSON(settings.json_src)
}
/**
* Receives data from _parseJSON() and asssigns to myJSON variable
* Assigns new response to myJSON2
*/
function _parseJSON2(data) {
myJSON = data;
const term = myJSON.value
return $.getJSON("http://website.com/" + term ".json")
// this is the then() you were missing
.then(function(data){
myJSON2 = data;
// optionally return myJSON2 to next `then()`
});
}
// $.when not really needed
_parseJSON()
.then( _parseJSON2 )
.then( _cacheOptions )
.then( _cacheDom )
.then( _events )
.then( _render );
根据mozila.org
如果使用fetch函数就不用jQuery
fetch("http://website.com/" + term ".json")
.then(function(response) {
return response.json();
})
.then(function(myJson) {
console.log(JSON.stringify(myJson));
});