echo json_encode($ array)没有返回我期望它返回的内容?

时间:2011-08-26 11:05:55

标签: jquery json

我有这段代码:

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

    function refresh()
    {      
        $.post('getMessageDetails.php', function (json) {
        $("#subject").html(json.subject);
        $("#subject_id").html(json.subject_id);
        $("#unique_code1").html(json.unique_code1);
        $("#unique_code2").html(json.unique_code2);
        $("#unique_code3").html(json.unique_code3);
        });   
        window.setTimeout(refresh,30000);
    }   
</script>

然后是我的HTML部分:

foreach ($row as $r)
{
   echo "<tr>";
   echo '<td><a id = "subject_id" class="red_link" href="'.ADDRESS.'view_message.php?id='.$r['id'].'"><span id = "subject">'.$r['subject'].'</span></a></td>';
   echo '<td id = "unique_code1">'.$uniqueCode1.'<span id = "unique_code2" class="pink_text">'.$uniqueCode2.'</span><span id = "unique_code3">'.$uniqueCode3.'</span></td>';
   echo "</tr>";
}

我的getMessageDetails.php:

<?php
header('Content-Type: application/json; charset=utf-8');
include('header_application.php');

$limit = 15;
if(!isset($_GET['page']))
   $page = 1;
else
   $page = $_GET['page'];

$from = (($page * $limit) - $limit);
$row = $obj_clean->getMessages($_SESSION['user_id'], $from, $limit);

if (count($row) > 0)
{
    foreach ($row as $r)
    {
        $codeLength = strlen($r['unique_code']);
        $codeLength = strlen($r['unique_code']);
        $firstPartLength = $codeLength - 5;
        $uniqueCode3 = substr($r['unique_code'], -2);
        $uniqueCode2 = substr($r['unique_code'], -5, 3);
        $uniqueCode1 = substr($r['unique_code'], 0, $firstPartLength);

        $message_id = $r['id'];
        $subject = $obj_clean->getMessageDetails($message_id);
        $opened_once = $obj_clean->getOpenedOnce($message_id);
        if ($opened_once >= 1)
        {   
            $array['subject'] = "blablabla";
            $array['subject_id'] = $r['id'];
            $array['unique_code1'] = $uniqueCode1;
            $array['unique_code2'] = $uniqueCode2;
            $array['unique_code3'] = $uniqueCode3;
            echo json_encode($r);
            exit();
        }
    }    
}
?>

我确实有echo json_encode($ r);                出口(); 在循环之外,不要以为那就是问题..问题在于json / json_encode但是我不知道要弄明白......可以帮助一下吗?

谢谢

1 个答案:

答案 0 :(得分:0)

我想你想要

echo json_encode($array);

而不是结果,

echo json_encode($r);