나는 나 자신과 친구를 위해 기본 채팅 시스템을 만들려고 노력하고있다. 매우 간단하지만, 아약스가 나를 상당히 혼란스럽게합니다. 저는 아약스를 사용하여 사용자 이름을 설정 한 PHP 페이지를 요청하고 마지막으로 채팅을 다운로드했습니다. 그러나, 나는 PHP 페이지 에코를 읽는 방법을 모른다. 온라인으로 확인했지만 많은 변형이 있기 때문에 혼란 스럽습니다. 누구든지 제발 좀 도와 줄래?jQuery AJAX를 통해 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");
이 있습니다. 내가 뭔가 잘못하고 있는거야?
인쇄 오류는 어디에서 발생 했습니까? 이후 ajax 호출 이후'latestTimestamp = data.message [0] .time;'하지만 데이터 변수는 더 이상 그 범위에 있지 않습니다! – ReGdYN