gpt4 book ai didi

jquery - 为什么alert(json[0].subject);给未定义?

转载 作者:行者123 更新时间:2023-12-01 00:25:09 25 4
gpt4 key购买 nike

我有这个代码:

 <script>
$(document).ready(function()
{
refresh();
});

function refresh()
{
$.get('getMessageDetails.php', function (json) {
alert(json);
alert(json[0].subject);
},"json");
window.setTimeout(refresh,30000);
}
</script>

然后我有 getMessageDetails.php:

<?php
//header('Content-Type: application/json; charset=utf-8');
include('header_application.php');
$lastNewMessageCnt = $obj_clean->getUnopenedMessagesCount($_SESSION['user_id']) + 1;
$last_unopened_message_row = $obj_clean->getLastUnopenedMessage($_SESSION['user_id'],$lastNewMessageCnt);
echo json_encode($last_unopened_message_row);
?>

然后我有警报(json),其中显示:

[{"subject":"Freechat ddd","id":"19","created_at":"2011-08-29 14:58:27","unique_code":"ALLEYCA000RC","opened_once":"0"}] 

正确的是

但是alert(json[0].subject);给出了未定义???

请帮忙?谢谢

最佳答案

您必须将 json 变量转换为正确的 json 格式变量。

目前它是一个字符串变量。

您必须按以下方式使用它:

<script>
$(document).ready(function()
{
refresh();
});

function refresh()
{
$.get('getMessageDetails.php', function (json) {
alert(json); // this is a string variable.
json = $.parseJSON(json); //now json varible is in correct json format.
alert(json.subject); //you can call it dirctly like a associative array. No need to include '[0]'.
},"json");
window.setTimeout(refresh,30000);
}
</script>

关于jquery - 为什么alert(json[0].subject);给未定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7230486/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com