getjson问题

时间:2011-08-22 07:01:52

标签: jquery getjson

var items;
$.getJSON('calender_service.php?command=calender', function(data) {
    items = data;
    var name = [];
    var date_start = [];

    var date_start_time = "";
    $.each(items, function(key, val) {
        name[key] = val.name;
        date_start[key] = val.date_start;
        events[key] = ""+""+","+val.date_start.split("-")+","+val.date_start_time+","+val.date_end_time+","+val.name+","+val.status+"";
    });

    alert(events);  
}); 

请帮助我,不要在getjson方法之外获取变量值事件。

4 个答案:

答案 0 :(得分:0)

为了使它在回调函数之外,你必须将events变量定义为全局变量(即在进行json调用之前尝试定义它)。

答案 1 :(得分:0)

试试这个:

var items;
var events;

$.getJSON('calender_service.php?command=calender', function(data) {
    items = data;
    var name = [];
    var date_start = [];
    var date_start_time = "";
    $.each(items, function(key, val) {
        name[key] = val.name;
        date_start[key] = val.date_start;
        events[key] = "" + "" + "," + val.date_start.split("-") + "," + val.date_start_time + "," + val.date_end_time + "," + val.name + "," + val.status + "";
    });
});

alert(events);

答案 2 :(得分:0)

将事件定义为外部范围中的变量。

var events = {};
var  items;
 $.getJSON('calender_service.php?command=calender', function(data) {
  items=data;
 var name=[];
  ...
  ..
});

答案 3 :(得分:0)

在$ .each循环之外定义“事件”。例如在date_start之后。