Javascript 如何在加载其他JSON后运行JSON
我使用与这个问题相同的解决方案。但是,我想在Javascript 如何在加载其他JSON后运行JSON,javascript,jquery,json,jsonp,getjson,Javascript,Jquery,Json,Jsonp,Getjson,我使用与这个问题相同的解决方案。但是,我想在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(s
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=\u parseJSON2(data.value)加载数据时必须运行code>,myJSON2
必须保存为变量,以便以后在代码中使用。
它更像另一个when()。然后()
是必需的
你知道怎么做吗
提前谢谢 您的$。当
实际上是冗余的,但这并不会真正导致问题
\u parseJSON2(data.value)
缺少的是将其作为承诺返回,同时将其响应分配给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 );
您的$。当
实际上是多余的,但这并不会真正导致问题
\u parseJSON2(data.value)
缺少的是将其作为承诺返回,同时将其响应分配给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));
});
根据mozila.org的说法
如果使用fetch函数,则无需使用jQuery
fetch("http://website.com/" + term ".json")
.then(function(response) {
return response.json();
})
.then(function(myJson) {
console.log(JSON.stringify(myJson));
});
谢谢你。只有一个问题\u parseJSON2
接受和arg,但我看到您在没有这个的情况下调用了它。可能吗?如果不是,它将得到什么值?它确实得到了它,因为它没有在中使用匿名函数,而是得到了匿名函数得到的参数。做一个data
的日志,其中myJSON=data
被分配…将在那里看到它谢谢。只有一个问题\u parseJSON2
接受和arg,但我看到您在没有这个的情况下调用了它。可能吗?如果不是,它将得到什么值?它确实得到了它,因为它没有在中使用匿名函数,而是得到了匿名函数得到的参数。在分配了myJSON=data
的地方记录data
,您将在那里看到它