duan19850312 2016-09-13 18:29
浏览 32

Ajax登录页面返回html标签

I am trying to make a simple login page using ajax and php. the response text it is returning is like this: responseText :

<html>
<head>
</head>
<body>
</body>
</html>
success

Why is this happening? How can I get rid of these html tags. my code is:

<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.js"></script>
</head>
<body>
<form action="" method="post" id="myform">
<label>Username</label><input type="text" id="username" name="user">
<label>Password</label><input type="text" id="password" name="pwd">
<input type="button" onclick="sub()" value="Submit">
</form>
</body>
</html>
            <script>
            function sub()
            {
            var email=$("#username").val();
            var password=$("#password").val();
            var dataString = 'email='+email+'&password='+password;
            if($.trim(email).length>0 && $.trim(password).length>0)
            {
            $.ajax({
            type: "POST",
            dataType: "text",
            url: "home1.php",
            data: "username=" + $("#username").val()+"&password=" +    $("#email").val(),
            cache: false,
            success: function(status){
                alert(status);
            }
            });

            }
                        }
</script>

home1.php:

<?php
include("db.php");
session_start();
if(isSet($_POST['email']) && isSet($_POST['password']))
{
$email=mysqli_real_escape_string($db,$_POST['email']); 
$password=mysqli_real_escape_string($db,$_POST['password']); 

$result=mysqli_query($db,"SELECT user_id FROM customers WHERE email1='$email' and password='$password'");
$count=mysqli_num_rows($result);

$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
if($count==1)
{
$_SESSION['id']=$row['user_id'];
echo "success";
}
}
?>
  • 写回答

2条回答 默认 最新

  • doujing9972 2016-09-13 18:35
    关注

    Your home1.php page must return a text, if you want alert(status) to work properly.

    Possibly, those tags might be inserted by your web server because you did not set the Content-Type properly.

    <?php
    // home1.php
    
        Header('Content-Type: text/plain;charset=utf8');
        die("This is the response");
    

    The above should work with your code.

    For a more advanced example, try removing the datatype 'text' and using JSON:

     success: function(response) {
         if (response.hasOwnProperty('status')) {
             // We should actually check there is a message property too...
             alert(response.message);
         } else {
             alert('Incorrect response received');
         }
     }
    

    and in home1.php start with a file containing just:

    <?php
        $response = array(
            'status'  => 'success',
            'message' => 'Everything is OK!',
        );
        Header('Content-Type: application/json;charset=utf8');
        die(json_encode($response));
    

    to see whether it all works.

    Also

    You're using jQuery, so you can save some coding:

    function sub() {
            var email=$("#username").val();
            var password=$("#password").val();
            var dataString = 'email='+email+'&password='+password;
            if($.trim(email).length>0 && $.trim(password).length>0) {
                $.post(
                    'home1.php',
                    {
                        username: email,
                        password: password
                    }
                ).done(function(response) {
                    // Here response is the JSON object returned by home1.php
                });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?