我是 jqeury ajax 新手,我尝试发布一些内容
testso.php
。我尝试在 testso.php 上回显 $_POST['test']
,但它没有显示任何内容。如果我打开开发人员工具并检查预览,我可以看到数据在那里。我是不是做错了什么?
测试.php
<script>
var test = '123';
$.ajax({
type: "POST",
url: "testso.php",
data: { test: test },
success: function(response) {
console.log(response);
}
});
</script>
testso.php
<?php
if(isset($_POST['test'])){
$img = $_POST['test'];
?>
<div><?php echo $img; ?></div>
<?php } ?>
我尝试使用
document.getElementById('ajaxreturn').innerHTML=test;
但这不是我想要的。也许有人可以建议我如何回应帖子。
使用PHP生成ajax使用的一些数据是完全有效且常见的。您的示例实际上可以工作,但结果不在页面上,而是在控制台上。
试试这个:
<script>
var test = '123';
$.ajax({
type: "POST",
url: "testso.php",
data: { test: test },
success: function(response) {
document.getElementById('ajaxreturn').innerHTML = response;
}
});
</script>
<div id="ajaxreturn"></div>
<?php
if(isset($_POST['test'])){
echo 'php ' . $_POST['test'];
}else{
echo "php not set.";
}