weixin_33724570 2016-05-31 23:22 采纳率: 0%
浏览 23

如何精确输出ajax

My code is working well but problem in output. I don't get exact output that i want.

$("#search_traveller_button").click(function(){
                $.ajax({
                    url: "index.php?act=checkSessionUser",
                    type: "POST",
                    cache: false,
                    success: function(data){
                            console.log(data);
                    },
                    error:function(){
                        console.log("Error: Unknown Error");
                    }
                });
            });

PHP code:

<?php
if(isset($_SESSION['userId'])) {
    echo "1";
} else {
    echo "0";
}
?>

output in success gives also html code, why?

0   </div>
        <footer class="nav navbar-inverse">
        ...........
        </footer>
    </body>
</html>

I want in my output only 0 in a variable, not html code.

  • 写回答

2条回答 默认 最新

  • weixin_33688840 2016-05-31 23:30
    关注

    Because the php is successful in returning a result.

    The error would be triggered if the php failed to return.

    If you want your Ajax handler to do something different if not logged in either specify in the Ajax handler (not recommended) or do it on the server side (in the php) returning what you want if they are not authenticated.

    $("#search_traveller_button").click(function(){
                $.ajax({
                    url: "index.php?act=checkSessionUser",
                    type: "POST",
                    cache: false,
                    success: function(data){
                        if (data==1){
                            console.log ("yeah it worked")
                        }else {
                            console.log ("error")
                        }
                });
            });
    
    评论

报告相同问题?

悬赏问题

  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复