Javascript - 不确定我的 AJAX 是否正确,没有错误,但没有数据库条目
我是一个 PHP 爱好者,对 javascript 非常陌生。我正在尝试向我正在构建的网站添加一个简单的 AJAX 聊天程序。问题是,我不知道如何测试我的请求是否已发送,或者是否是其背后的 PHP 出了问题。如果是 PHP,我可以解决这个问题,如果是 javascript,那么,这就是这个问题的目的。我没有收到任何错误,但我的数据库中也没有收到任何聊天消息。这是我在聊天页面上的 JavaScript:
<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 调试方法不起作用,因为它们由带有模具消息或 vardumping 预期值等的半分割问题区域组成 - 所有这些在页面加载后都不会真正显示,对吗?所以这让我无法区分是 PHP 还是 javascript。无论如何,感谢您的帮助。
I'm a PHP guy and am extremely new to javascript. I'm trying to add a little simple AJAX chat program to a website I'm building. Problem is, I don't know how to test whether or not my requests got sent or if it's the PHP behind it that is the problem. If it's the PHP I can fix that, if it's the javascript, well, that's what this question is for. I'm not getting any errors but I'm also not getting any chat messages in my database. Here's my javascript on the chat page:
<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>
I'm sure there are better ways to write the code, but can anyone see any errors in it? My usual PHP debugging methods don't work as they consist of half-splitting problem areas with die messages or vardumping expected values, etc - all of which wouldn't really display after the page is loaded, correct? So that leaves me unable to half-split between it being the PHP or the javascript. Anyway, thanks for any help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一个重要的调试工具是 Firebug,它是 Firefox 的插件。
您不仅可以使用它来检查 HTML,还可以查看 XMLHTTP 请求的情况。
安装完成后,您将在右下角看到萤火虫图标。打开它,启用所有内容并选择 NET 选项卡。您可以从那里开始探索。
One important debugging tool is Firebug, which is a plug-in for Firefox.
You can use it not only to inspect your HTML but also to watch what's going on with your XMLHTTP request.
Once installed, in the lower-right corner you'll see the firebug icon. Open it up enable everything and select the NET tab. You can start poking around from there.
另外,在IE9中您可以使用F12来调试和查看错误。
Also, in IE9 you can use F12 to debug and view errors.
我看不到在那段代码中的任何地方调用 sendMessage() 。
I can't see sendMessage() being invoked anywhere in that piece of code.
您的代码只有一个错误... document.getElementByID("xxx") 不是该方法。它是 document.getElementById("xxx")。我尝试了你的代码 - 它回发到tinChat_process.php,参数为action = newMessage和message =“无论你在那个框中输入什么......”..如果你仍然无法解决,你可以发布chatMess和ajaxBox并告诉我们如何做您正在调用 sendMessage() 吗?我添加了一个按钮来测试通话:
Your code has only one error... document.getElementByID("xxx") is not the method. It is document.getElementById("xxx"). I tried your code -- it posts back to tinChat_process.php with parameters action = newMessage and message = "Whatever you type in that box...".. If you still cannot resolve can you post the chatMess and ajaxBox and tell us how you are calling the sendMessage()? I added a button to test the call: