全局使用getJson的响应

时间:2016-09-21 18:19:40

标签: jquery json getjson

当页面加载时我想得到一个json:

$.getJSON(_routes['salesInAMonthJS'], { month: "September 2016" }).then(
    function(d){
        console.log(d);
    }
);

这个

$.getJSON(_routes['salesInAMonthJS'], { month: "September 2016" });

让我回来

{"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);
    });
});

然而,这似乎不起作用,任何想法我做错了什么?感谢

2 个答案:

答案 0 :(得分:0)

这样的东西?

$.getJSON(geocodingAPI, function(json) {
  $.json = json;
});

$('button').on('click', function() {
  alert($.json.status)
});

http://jsfiddle.net/eywpvyrr/1/

答案 1 :(得分:0)

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);
        });
    });
});