【发布时间】:2011-06-02 14:16:02
【问题描述】:
我正在尝试使用 JSON 获取动态内容到我的网页,使用 javascript。有些东西不正确,我无法弄清楚它可能是什么。在萤火虫中,我可以看到 JSON 数据已按应有的方式检索。在“DOM”下的 Firebug 中,我正在访问的页面 URL(我创建的实际页面,而不是 JSON 数据的 URL)显示为红色(见下面的屏幕截图)。这是我的javascript:
$(document).ready(function() {
$('#target').click(function() {
alert("At least I',m reached ");
$.getJSON('http://localhost/timereporting/phpscriptlibrary/get_remaining_hours.php', function(data) {
document.getElementById('errorDiv').innerHTML = "Divtext";
alert("Inside getJason");
});
alert("At least I',m done ");
});
这是我的 php 文件的重要部分:
$json_string = "{\"activities\": ";
$json_string = $json_string."[";
for ( $counter = 0; $counter < $num; $counter += 1) {
$json_string = $json_string."[".mysql_result($rows,$counter,'date').", \"".mysql_result($rows,$counter,'activity_id')."\", ".mysql_result($rows,$counter,'hours')."]";
if($counter != ($num-1)){
$json_string = $json_string.", ";
}
}
$json_string = $json_string."]}";
echo $json_string;
我假设“echo”是将 JSON 数据“发送”到 javascript 的方式?
奇怪的是,在 firebug 中 JSON 数据以两种不同的方式呈现。如果您查看下面包含的屏幕截图,第二个的日期类似于“1988”或类似的日期,而第一个的日期更完整,例如“2010-12-10”。第一个屏幕截图描述了它应该是怎样的,这就是我试图发送它的方式,显然它在某些时候是这样接收的。
为什么我的 div-tag 没有更新日期,或者 $.getJSON 中的警报没有触发,只有之前和之后的警报?
【问题讨论】:
标签: php javascript json dom