我用ajax这样获得来自服务器的数据获得的数据:(编者)
$(document).ready(function()
{
setInterval(function()
{
$.get('/forms/requestProcessor.php', function(data)
{
$("#shout").append(data.question+'<br>');
alert('Load was performed.');
},'JSON');
}, 5000); // 5 seconds
});
在PHP文件,我发送的数据是这样的:
while($row=mysql_fetch_array($result))
{
$question=$row['Question'];
$choice1=$row['Choice1'];
$choice2=$row['Choice2'];
$choice3=$row['Choice3'];
$choice4=$row['Choice4'];
$return_data->question = $question;
$return_data->choice1 = $choice1;
$return_data->choice2 = $choice2;
$return_data->choice3 = $choice3;
$return_data->choice4 = $choice4;
echo(json_encode($return_data));
}
它打印“未定义”。但是,如果我将浏览器直接指向php文件,它将以json格式正确显示数据
我做了它的工作使用$ .getJSON函数,而不是使用$不用彷徨()函数......然而,正如我在上面所指出的,如果我使用回声里面的while循环,它不工作...如果我在while循环外使用回声,我将只得到我的查询的最后一行...有人能告诉我如何克服这个问题? – CuriousCoder 2011-01-31 04:44:21