drix47193 2019-04-21 21:36
浏览 267
已采纳

始终在pushJsonData中收到错误:parsererror:SyntaxError:我脚本中JSON输入[object Object]的意外结束

I always receive the Parser error in pushJsonData and I don't know why.

I tested it first with just the user value and it works fine, but when I try to thrwo back mor than the user I recieve the error below:

SyntaxError: Unexpected end of JSON input [object Object]

My JavaScript code:

$('#Login').click(function() {
    var user = $("#user").val();
    $.ajax({
        url: "src/ajax.php",
        type: "POST",
        data: { ACTION : "checkUser", USER: user},
        dataType: "json",
        success: function(data)  {
            var user = data.user;
            $('#testUser').text("Hallo " + user);
        },

        error: function(jqXHR, textStatus, errorThrown) {
            alert(textStatus + " in pushJsonData: " + errorThrown + " " + jqXHR);
        }
    });
});

My PHP code:

if($_POST['ACTION'] == "checkUser") {
    $checkUser      = $_POST['USER'];
    $sql            = $db->query("SELECT * FROM fm_user WHERE user = '".$checkUser."' LIMIT 1");
    $getUser        = $sql->fetch_assoc();

    $resultArray    = array(
                        "user" => $getUser['user'],
                        "isadmin" => $getUser['isadmin'],
                        "user_data" => $getUser['user_data']
                    );

    $result = json_encode($resultArray);
}
  • 写回答

1条回答 默认 最新

  • drduh44480 2019-04-21 22:02
    关注

    You're not outputting anything. But, rather than close this as off-topic due to a typo I wanted to point out some issues with your code, which is unsafe and inefficient. Always always use prepared statements, and there's no need to waste code putting array elements into an array. Something like this should work better:

    <?php
    if($_POST['ACTION'] == "checkUser") {
        $result = null;
        $checkUser = $_POST['USER'];
        $stmt = $db->prepare("SELECT user, isadmin, user_data FROM fm_user WHERE user = ? LIMIT 1");
        $stmt->bind_param("s", $checkUser);
        if ($stmt->execute()) {
            $result = $stmt->get_result();
            $getUser = $result->fetch_assoc();
    
            $result = $getUser;
        }
        header("Content-Type: application/json");
        echo json_encode($result);
        die();
    }
    

    Untested, and it's been a long time since I've used mysqli but it should work.

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

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择