如何在没有页面的情况下使AJAX读回响应's源并且没有JSON

How to make AJAX read back response without page's source and without JSON?

本文关键字:响应 JSON 回响 AJAX 情况下      更新时间:2023-09-26

我使用以下代码提交表单:

$.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!"; 
}

JS弹出sure!和整个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(); 
}
if (isset($_POST["num"])) {
    $num = $_POST["num"];
    if($num == 1) 
        echo "sure!";
    return; 
}