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")
                        }
                });
            });
    
    评论

报告相同问题?