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.

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

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!