dongzhiqi0332 2014-10-27 14:13
浏览 33
已采纳

如何在没有页面源和没有JSON的情况下进行AJAX回读响应?

I use the following code to submit a form:

$.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 {}
    }
});

Then in the PHP index.php I have:

if (isset($_POST["num"])) {
    $num = $_POST["num"];
    if($num == 1) 
        echo "sure!"; 
}

The JS popups with the sure! and the whole index.php page source code. Is there a way to remove the source code and just have the response show without setting the dataType to JSON?

  • 写回答

2条回答 默认 最新

  • doude4924 2014-10-27 14:16
    关注

    Simply stop script execution after output:

    //must be before any other output, eg at top of file
    if (isset($_POST["num"])) {
        $num = $_POST["num"];
        if($num == 1) 
            echo "sure!";
            die(); 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写