Jquery 全局使用来自getJson的响应
加载页面时,我希望获得json,如下所示:Jquery 全局使用来自getJson的响应,jquery,json,getjson,Jquery,Json,Getjson,加载页面时,我希望获得json,如下所示: $.getJSON(_routes['salesInAMonthJS'], { month: "September 2016" }).then( function(d){ console.log(d); } ); 这个 还给我 {"9":{"name":"alex lloyd","country":"Germany","antiquity":"new client","amount":"0.0 USD"},"10":{
$.getJSON(_routes['salesInAMonthJS'], { month: "September 2016" }).then(
function(d){
console.log(d);
}
);
这个
还给我
{"9":{"name":"alex lloyd","country":"Germany","antiquity":"new client","amount":"0.0 USD"},"10":{"name"
:"asdasdsadasda dasda","country":"Afghanistan","antiquity":"new client","amount":"0.0 USD"},"11":{"name"
:"Alex Lloyd","country":"American Samoa","antiquity":"new client","amount":"0.0 USD"},"12":{"name":"alex
lloyd","country":"Aruba","antiquity":"new client","amount":"0.0 USD"},"5":{"name":"surgeon bueno","country"
:"Spain","antiquity":"renewal","amount":"2686.97 USD"}}
但是,当用户单击按钮时,我希望显示存储在d
例如:
$(document).on("click ", ".tick", function(e) {
e.preventDefault();
$.each(d, function(i, item) {
&("div.container").append(d[i].name);
});
});
然而这似乎不起作用,你知道我做错了什么吗?谢谢诸如此类的事
$.getJSON(geocodingAPI, function(json) {
$.json = json;
});
$('button').on('click', function() {
alert($.json.status)
});
像这样的事
$.getJSON(geocodingAPI, function(json) {
$.json = json;
});
$('button').on('click', function() {
alert($.json.status)
});
d的作用域只是回调函数。您可以将单击绑定移动到该位置,以便访问它
$.getJSON(_routes['salesInAMonthJS'], { month: "September 2016" }).then(function(d){
$(document).on("click ", ".tick", function(e) {
e.preventDefault();
$.each(d, function(i, item) {
$("div.container").append(d[i].name);
});
});
});
d
的作用域只是回调函数。您可以将单击绑定移动到该位置,以便访问它
$.getJSON(_routes['salesInAMonthJS'], { month: "September 2016" }).then(function(d){
$(document).on("click ", ".tick", function(e) {
e.preventDefault();
$.each(d, function(i, item) {
$("div.container").append(d[i].name);
});
});
});
&(“div.container”)
是对$(“div.container”)
的输入错误。您有一个可变范围问题d
是$回调函数的本地对象。getJSON
回调函数无法在其他函数中访问它。&(“div.container”)
是$(“div.container”)
的输入错误。存在变量范围问题d
是$回调函数的本地函数。getJSON
回调函数不能在其他函数中访问它。