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 cplex运行后参数报错是为什么
  • ¥15 之前不小心删了pycharm的文件,后面重新安装之后软件打不开了
  • ¥15 vue3获取动态宽度,刷新后动态宽度值为0
  • ¥15 升腾威讯云桌面V2.0.0摄像头问题
  • ¥15 关于Python的会计设计
  • ¥15 聚类分析 设计k-均值算法分类器,对一组二维模式向量进行分类。
  • ¥15 stm32c8t6工程,使用hal库
  • ¥15 找能接spark如图片的,可议价
  • ¥15 关于#单片机#的问题,请各位专家解答!
  • ¥15 博通raid 的写入速度很高也很低