为什么它不显示“你错了”消息


why it doesn't display "you had wrong" message?

我经常通过jquery使用ajax,但根据下面的代码,为什么当"IF"是真的时它不显示"你有错"的消息? 并且只显示"失败"消息?

文件登录.php

<!DOCTYPE html>
<html lang="en" class="no-js">
<head>
<title>just for test</title>
</head>
<div id="Response" ></div>
<h3>login</h3>
<form id="Login" class="form-login">
 <input type="text" class="form-control" name="yourname" id="yourname" placeholder="yourname">
 <button type="submit" class="btn btn-bricky pull-left"> ورود <i class="fa fa-arrow-circle-right"></i> </button>
</form>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script> 
<script>
$(document).ready(function (e){
$("#Login").on('submit',(function(e){
e.preventDefault();
    $.ajax({
        url: "include/php/check_login.php",
        type: "POST",
        data:  new FormData(this),
        contentType: false,
        cache: false,
        processData:false,
        success: function(data)
        {
            if(data=='faile'){
             $("#Response").html('you had wrong...');
            }else{
             $("#Response").html(data);
            }
              return false;
            }       
        });
}));
});        
    </script>
</body><!-- end: BODY -->
</html>

文件:check_login.php

<?php
if (isset($_POST['yourname']) && !empty($_POST['yourname']))
{
    $yourname=$_POST['yourname']; 
    echo $yourname;
}
else echo "faile";
?>

这是接收基于 json 的数据的方法

<!DOCTYPE html>
<html lang="en" class="no-js"> <head> <title>just for test</title> </head>
    <body>
        <div id="Response" ></div>
        <h3>login</h3>
        <form id="Login" class="form-login">
             <input type="text" class="form-control" name="yourname" id="yourname" placeholder="yourname">
             <button type="submit" class="btn btn-bricky pull-left"> ورود <i class="fa fa-arrow-circle-right"></i> </button>
        </form>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script> 
        <script type="text/javascript">
            $(function (e) {
                $("#Login").submit(function(e){
                e.preventDefault();
                $.ajax({
                    url: "check_login.php",
                    data:  new FormData(this),
                    type: "POST",
                    contentType: false,
                    cache: false,
                    processData:false,
                    // data type should be set to json to get the response data as json value
                    dataType: 'json',
                    success: function(json) {
                            if( json.data == 'faile') {
                                $("#Response").html('you had wrong...');
                            } else {
                                $("#Response").html(json.data);
                            }
                            return false;
                        }
                    });
                });
            });
        </script>
    </body>
</html>

check_login.php

<?php
    session_start();
    if (isset($_POST['yourname']) && !empty($_POST['yourname']))
    {
        $response = array( "data" => $_POST['yourname']);
    } else {$response = array( "data" => "faile");}
    echo json_encode($response);
?>

使用 json,应该没有关于'n的错误::回显后的新行

地点

var yourname=$('#yourname').val();

介于<script>$(document).ready(function (e){之间

并从

data:  new FormData(this),

data: "yourname="+yourname;

else echo "faile";后添加die();

查找在

?>之后返回的换行符,因为它返回类似 "faile'n" 的内容。

您可以使用以下方法之一:

  • 删除结束?>标记
  • echo后添加die
  • 检查"faile'n"(不推荐)