jquery jqrowl从getjson获取消息

时间:2013-07-12 14:12:10

标签: jquery json jgrowl

首先,我是一名PHP开发人员,我正在尝试学习jquery(javascript),到目前为止我已经学到了很多,但我有点迷失。

json输出。

{"msg": [{"message":"blah", "header":"title of blah"}, {"message":"blah", "header":"title of blah"}]}

我之所以这样,是因为我想要这样的东西。

$message = blah

$header = title of blah

而不是

message = message

header = header

这是我通过使用....

$.getJSON("json.php", function(data) {
$.each(data, function(key, val) {
$.jGrowl(key, { 
life: 5000,
header: val,
});
});
});

我知道,我需要添加另一个$.each,这就是我失去了一点的地方。我需要更多像这样的东西,但是在javascript中。

$msg = array("message" => 'blah', "header" => 'title blah' );

$message = $msg['message']; // blah
$header = $msg['header']; // title of blah

1 个答案:

答案 0 :(得分:0)

我不确定我是否理解得很好。但是你不再需要1个"每个"循环我想。以下代码适合您吗?

$.getJSON("json.php", function(data, textStatus, jqXHR) {
  // if you only put data as input, data variable is an array like [data, textStatus, jqXHR] I think. Thus the "each" function returns you the data, the status and the xhr I think
  $.each(data.msg, function(index, value) {
    // for each entry of the msg array, create a notification. "value" is a {"header": "...", "content": "..."} object
    $.jGrowl(
      value.message, // here is the notification's content
      {
        group: "myhtmlclass",
        life: 5000,
        header: value.header, // here is the notification's title
      }
    });
  });
});
相关问题