doumibi6899 2017-07-06 19:19
浏览 75
已采纳

JSON响应显示为true↵,长度错误

I am new to AJAX and making a simple login system with AJAX and PHP. My JSON response shows as true↵ with length 6. Where is the problem, and how can I solve it?

AJAX

$('document').ready(function() {
  $("#login-form").on("submit", (function(e) {
    e.preventDefault();
    var data = $("#login-form").serialize();

    $.ajax({
      type: 'POST',
      url: 'login-response.php',
      data: data,

      success: function(response) {
        console.log(response.length);

        if (response == true) {
          $("#btn-login").html('<img src="btn-ajax-loader.gif" /> &nbsp; Signing In ...');
          setTimeout(' window.location.href = "demo.php"; ', 4000);
        } else {
          $("#error").fadeIn(1000, function() {
            $("#error").html('<div class="alert alert-danger"> <span class="glyphicon glyphicon-info-sign"></span> &nbsp; ' + response + ' !</div>');
            $("#btn-login").html('<span class="glyphicon glyphicon-log-in"></span> &nbsp; Sign In');
          });
        }
      }
    });
  }));
});

login-response.php

session_start();
$user_email = $_POST['user_email'];
$user_password = $_POST['password'];

if ($user_email == 'admin' && $user_password == '123') {
    $_SESSION['user_session'] = $user_email;
    echo json_encode(true);
}
else {
    echo "email or password does not exist."; // wrong details
}
  • 写回答

1条回答 默认 最新

  • doushao6874 2017-07-06 19:32
    关注

    As per my comment: json_encode(true) will not return a valid JSON. Instead, you will want to encode an array of sorts. There are many ways to do it, but one way is to simply encode an array that is ['success' => true], in other words:

    session_start();
    $user_email = $_POST['user_email'];
    $user_password = $_POST['password'];
    
    if ($user_email == 'admin' && $user_password == '123') {
        $_SESSION['user_session'] = $user_email;
        echo json_encode(array('success' => true));
    }
    else {
        echo json_encode(array('success' => false, 'message' => 'email or password does not exist.'));
    }
    

    Note that you should also encode your failure message, because your jQuery expects to receive something in JSON.

    Once you receive this response in your jQuery logic, you can simply check if the response has a success object with the value of true, i.e.:

    $.ajax({
        type: 'POST',
        url: 'login-response.php',
        data: data,
        dataType: 'json'
    })
    .done(function(response) {
        if (response.success) {
            $("#btn-login").html('<img src="btn-ajax-loader.gif" /> &nbsp; Signing In ...');
            setTimeout(' window.location.href = "demo.php"; ', 4000);
        } else {
            $("#error").fadeIn(1000, function() {
                // Your error message is accessible via `response.message`
                $("#error").html('<div class="alert alert-danger"> <span class="glyphicon glyphicon-info-sign"></span> &nbsp; ' + response.message + ' !</div>');
                $("#btn-login").html('<span class="glyphicon glyphicon-log-in"></span> &nbsp; Sign In');
            });
        }
    });
    

    Some notes:

    • Request status is accessible via response.success. Of course, depending on personal preference, you may have alternatives in returning exit status, such as using an exit code (0 indicates ok, anything other than 0 means an error)
    • Your failure response message will be accessible via response.message

    p/s: Remember that the jqXHR.success method is deprecated! Use jqXHR.done() instead ;)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器