我无法使用JQuery/Ajax将PHP文件的内容加载到div标记中。
这是我加载文件的页面:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script>
function init() {
reloadChat();
setInterval (reloadChat, 5000);
}
function reloadChat() {
$.ajax({
url: "chat.php",
cache: false,
success: function(){
$("#chatmenu").load("chat.php");
},
});
}
</script>
<body onLoad='init()'></body>
<div id='chatmenu'></div>
我正在加载的PHP文件(chat.php)也在同一个文件夹中,它只是一个echo语句:
<?php echo "test"; ?>
为了确保这不是我的Javascript函数的问题,我在success函数下添加了一个警告,它确实每5秒提醒我一次,所以我认为这是与load语句有关。
发布于 2012-07-15 23:53:07
直接使用.load()
,不需要先发出Ajax请求:
function reloadChat() {
$("#chatmenu").load("chat.php");
}
更新:
我注意到在示例代码中,主体标记在div
元素之前结束。
<body onLoad='init()'></body> <!-- This ain't right -->
<div id='chatmenu'></div>
试着这样做:
<body onLoad='init()'>
<div id='chatmenu'></div>
</body>
发布于 2012-07-15 23:53:24
试试这个:
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script>
function init() {
reloadChat();
setInterval (reloadChat, 5000);
}
function reloadChat() {
$.ajax({
url: "chat.php",
cache: false,
success: function(response){
$("#chatmenu").text(response);
},
});
}
</script>
<body onLoad='init()'></body>
<div id='chatmenu'>
</div>
另外,看在上帝的份上,请使用最新版本的jQuery
发布于 2012-07-16 00:08:15
看起来你的第一个来自测试的请求将返回‘$.ajax
’,然后你使用它作为$("#chatmenu").load
的URL。
试试这个:
function reloadChat() {
$.ajax({
url: "chat.php",
cache: false,
success: function(data){
$("#chatmenu").append(data);
},
});
}
或者,如果您想替换#chatmenu
的内容,Christofer Eliasson发布的方法也可以使用,您只需在reloadChat
中调用$("#chatmenu").load("chat.php")
即可。
https://stackoverflow.com/questions/11493314
复制相似问题