您可以让PHP脚本返回JSON响应。
如果您需要在脚本中添加复杂性,它可能看起来像很多代码,但绝对值得。
1-确保无论发生什么都不会缓存响应:
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
2- json类型标题:
header('Content-type: application/json');
3-计算您需要知道的每个值:
$totalMsg = $mysql->totalRows("SELECT msg_id from messages WHERE msg_opened = 0 AND msg_receiver = '".$_SESSION["ActiveUserSessionId"]."'");
4-用它们构建一个数组:
$response = array(
'total' => $totalMsg,
'extra' => 'extra value (if needed)'
);
json编码中的
5- echo:
echo json_encode($response);
之后,您可以使用jQuery访问您的值:
$.ajax({
type: "POST",
url: '/BubbleStat.php',
cache: false,
dataType: 'json',
success: function(jsonData)
{
if (jsonData.total != null && jsonData.total != undefined)
{
$("#Bubble_Msg").text(jsonData.total).show();
}
}
});
如果要使用变量,问题会变得更容易。