Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 全局使用来自getJson的响应_Jquery_Json_Getjson - Fatal编程技术网

Jquery 全局使用来自getJson的响应

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":{

加载页面时,我希望获得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":{"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
回调函数不能在其他函数中访问它。