我是一個PHP傢伙,並且對JavaScript非常新穎。我正在嘗試爲我正在構建的網站添加一個簡單的AJAX聊天程序。問題是,我不知道如何測試我的請求是否已發送,或者如果它是PHP背後的問題。如果是我可以修復的PHP,如果它是JavaScript,那麼這就是這個問題的用意。我沒有收到任何錯誤,但我也沒有收到我的數據庫中的任何聊天消息。這裏是我的聊天頁面上的javascript:Javascript - 不確定我的AJAX是否正確,沒有錯誤,但沒有數據庫條目
<script type="text/javascript">
var xmlhttp;
var newChatMessage;
window.onunload=function() {
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
}
else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("POST","tinChat_process.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("action=pageClose");
};
function loadChat() {
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
}
else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("POST","tinChat_process.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send("action=grabChat");
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("ajaxBox").innerHTML=xmlhttp.responseText;
}
}
t=setTimeout("loadChat()",5000);
}
function sendMessage() {
newChatMessage = "action=newMessage&message=" + document.getElementByID("chatMess").value;
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
}
else {
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("POST","tinChat_process.php",true);
xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
xmlhttp.send(newChatMessage);
xmlhttp.onreadystatechange=function() {
if (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("ajaxBox").innerHTML=xmlhttp.responseText;
}
}
}
loadChat();
</script>
我敢肯定有更好的方法來寫代碼,但任何人都可以看到它的任何錯誤?我通常的PHP調試方法不起作用,因爲它們包含半分割的問題區域,包含die消息或vadumping期望值等 - 所有這些在頁面加載後都不會真正顯示,是正確的?所以這讓我無法在PHP或JavaScript之間進行一半的分割。無論如何,謝謝你的幫助。
通過在jQuery上進行1小時的速成課程來幫助自己。它可以節省你的工作時間。 http://docs.jquery.com/Tutorials – 2011-03-29 03:44:37