我使用以下代码提交表单:
$.ajax({
url: $('#myForm').attr('action'),
url: 'index.php',
type: 'post',
/*dataType: 'html json', *///tell the server that you expect json
data: {
"num" : $('#myNum').val(),
},
success: function(response) {
alert(response);
if ( check_validate() == 1) {
alert(response);
}
else {}
}
});
然后在 PHP index.php
中我有:
if (isset($_POST["num"])) {
$num = $_POST["num"];
if($num == 1)
echo "sure!";
}
带有 sure!
的 JS 弹出窗口和整个 index.php
页面源代码。有没有办法删除源代码并只显示响应而不将 dataType
设置为 JSON
?
最佳答案
输出后简单地停止脚本执行:
//must be before any other output, eg at top of file
if (isset($_POST["num"])) {
$num = $_POST["num"];
if($num == 1)
echo "sure!";
die();
}
关于javascript - 如何在没有页面源且没有 JSON 的情况下使 AJAX 读回响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26589492/