我试图为自己和朋友制作一个基本的聊天系统。这很简单,然而,ajax让我非常困惑。我使用ajax来请求一个php页面,我设置了一个用户名,以及我最后一次下载聊天。但是,我不知道如何阅读php页面的回声。我已经在网上查了一下,但是有很多变化,让我更加困惑。有人可以给我一些指导吗?
Scripts.js(已经链接了jQuery)
// on document ready + called every second
$.ajax({
url: 'download.php',
type: 'REQUEST',
datatype: 'json',
data: ({
last_downloaded: latestTimestamp,
username: username
}),
success: function (data) {
console.log(data.message[0].time);
// Uncaught TypeError: Cannot read property '0' of undefined
// how do I get the time from the json?
// {"message":{"chat":"Hello, world!","time":"2014-05-09 17:32:00,"username":"Josue"},"message":{"chat":"This is my second message.","time":"2014-05-09 17:32:05,"username":"Josue"}}
}
});
latestTimestamp = data.message[0].time;
downloadingTimer = setTimeout(actuate, timeoutValues[currentTimeoutIndex]);
}
的download.php
这是我的php页面作为普通字符串回显(现在格式正确的JSON):
[
{
"chat":"Hello, world!",
"time":"2014-05-09 17:32:00",
"username":"Josue"
},
{
"chat":"This is my second message.",
"time":"2014-05-09 17:32:05",
"username":"Josue"
}
]
我的页面顶部有header("Content-Type: application/json");。我做错了什么?