我正在制作一个代码,其中数据库中的所有注释都在AJAX的帮助下得到回应 . 现在我有两个问题 .
- 位置不起作用 . 这是我打开xmlHttpRequest的代码 .
xmlHttp.open("GET", "location.href.split('/').pop();", true);
当我检查状态是否= = 200时,我收到“未找到”错误 . 但是当我使用文件名时,我知道它在工作目录中 . 我必须使用类似上面的内容,因为我在url中有一些设置变量来创建唯一页面 .
- 评论没有回应 . 这是我的
PHP
<?php
$showcomment=mysql_query("SELECT * FROM `photosite`.`comments` WHERE `photo_id`='$photo_id'");
echo '<response>';
while($allcomments=mysql_fetch_array($showcomment)){
echo '<div id="comment"></div>';
}
echo '</response>';
?>
Javascript
function process(){
if(xmlHttp){
try{
xmlHttp.open("GET", "location.href.split('/').pop();", true);
xmlHttp.onreadystatechange = handleServerResponse;
xmlHttp.send(null);
}catch(e){
alert( e.toString() );
}
}
}
function handleServerResponse(){
comment = document.getElementById('comment');
if(xmlHttp.readyState==4){
if(xmlHttp.status==200){
try {
comment.innerHTML += "'.$allcomments['firstname'].' '.$allcomments['surname'].' '.$allcomments['comment'].'<br>";
}catch(e){
alert( e.toString() );
} else {
alert(xmlHttp.statusText );
}
}
}
}
在innerHTML上,我所做的就是回应我没有AJAX的回声 . 它出现在页面上但是就是这样 . 如何让它识别它们是PHP变量?一旦它们在PHP页面上回显,它们不应该变成变量吗?
1 回答
删除JavaScript表达式周围的引号,否则只需要一个字符串 . 那就是说,您是否正在尝试将AJAX发送到当前页面?在这种情况下,只需
location.href
即可 .实际上,这很可能是你所有问题的根源 .