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

始终在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.

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

报告相同问题?

悬赏问题

  • ¥15 输出区间内所有的完数的个数。 要简单点的程序
  • ¥15 asp.core 权限控制怎么做,需要控制到每个方法
  • ¥20 while循环中OLED显示中断中的数据不正确
  • ¥15 这个视频里的stm32f4代码是怎么写的
  • ¥15 JNA调用DLL报堆栈溢出错误(0xC00000FD)
  • ¥15 请教SGeMs软件的使用
  • ¥15 自己用vb.net编写了一个dll文件,如何只给授权的用户使用这个dll文件进行打包编译,未授权用户不能进行打包编译操作?
  • ¥50 深度学习运行代码直接中断
  • ¥20 需要完整的共散射点成像代码
  • ¥15 编写vba代码实现数据录入工作