我在页面上有一个元素,当单击该元素时会向receiver.php
文件发出ajax请求:
<script>
function send(id){
$.ajax({
type: 'POST',
dataType: 'html',
url: 'receiver.php',
data: {id: id}
});
}
</script>
<img src="foo.bar" onclick="send(id)"/> <!-- simplified -->
我的想法是,该receiver.php
文件将接收id
,然后根据该文件输出整个HTML页面id
但是,当我单击该元素时,预期的新HTML页面不会显示。当我转到Chrome检查器的“网络”选项卡时,可以看到请求,并且响应正是我需要的HTML内容,但是为什么不将其更改为新页面并保留在旧页面上呢?
编辑:这是我的receiver.php
,用于测试目的:
<html>
<head></head>
<body>
<?php
echo "<p>".$_POST['comid']."</p>";
echo "<p> foo foo </p>";
?>
</body>
</html>
这是响应:
<html>
<head></head>
<body>
<p>3</p><p> foo foo </p> </body>
</html>
也许在receiver.php
脚本中您正在执行类似的操作,在该脚本中,根据要接收的ID确定要输出的HTML页面。
switch ($_POST['id']) {
case 'foo':
$filepath = 'bar';
break;
...
}
readfile($filepath);
您必须使用的功能将其反映在AJAX查询success
中$.ajax
。
function send(id) {
$.ajax({
type: 'POST',
dataType: 'html',
url: 'receiver.php',
data: {id: id},
success: function (data) {
// Replace the whole body with the new HTML page
var newDoc = document.open('text/html', 'replace');
newDoc.write(data);
newDoc.close();
}
});
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句