【问题标题】:jquery jqrowl get message from getjsonjquery jqrowl 从 getjson 获取消息
【发布时间】:2013-07-13 01:07:46
【问题描述】:

首先,我是一名 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

【问题讨论】:

  • 我不太确定您面临什么错误。你能把它放在小提琴上吗?
  • 我会但我没有收到错误。我想弄清楚或学习如何正确地做。

标签: jquery json jgrowl


【解决方案1】:

我不确定我是否理解得很好。但我认为你不需要再有 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
      }
    });
  });
});

【讨论】:

    猜你喜欢
    • 2023-03-22
    • 1970-01-01
    • 2019-07-17
    • 2014-07-12
    • 2016-03-10
    • 1970-01-01
    • 2013-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多