JSON SyntaxError:JSON输入的意外结束(...)未定义

时间:2016-05-19 07:33:45

标签: php jquery json

现在正在阅读和搜索两天,并尝试查看所有其他答案和问题,但我无法弄清楚。也许我在代码中遗漏了一些小东西。

我得到了JSON SyntaxError: Unexpected end of JSON input(…) undefined。我知道这可能与某个地方的JSON有关,比如错过了{或a",但我看不到它。

这是我的JS;

$("#btnUpdateInformation").on("click", function (e) {
    var formData = {
        'user_id'                     : $("#hiddenUserID").val(),
        'userPassword'                : $('#userPassword').val(),
        'userRetypePassword'          : $('#userRetypePassword').val(),
        'userBirthCountry'            : $('#userBirthCountry').val(),
        'userBirthCity'               : $('#userBirthCity').val(),
        'userBirthAddress'            : $('#userBirthAddress').val(),
        'UserZipCode'                 : $('#UserZipCode').val(),
        'userFirstName'               : $('#userFirstName').val(),
        'userLastName'                : $('#userLastName').val(),
        'userSex'                     : $('#userSex').val(),
        'userBirthDay'                : $('#userBirthDay').val(),
        'userBirthMonth'              : $('#userBirthMonth').val(),
        'userBirthYear'               : $('#userBirthYear').val(),
        'userPhoneNr'                 : $('#userPhoneNr').val(),
        'userPasswordConfirm'         : $('#userPasswordConfirm').val()
    };
    console.log(formData);
    $.ajax({
    type: "POST",
    url: "PHP/updateUserProfile.php",
    data: formData,  
    success: function(data){
        console.log(data);
        if(data.status == 'success'){
        console.log("success");
        }else if(data.status == 'error'){
        console.log("error");
        }else if(data.status == 'no_results'){
        console.log("no results");
        }else if(data.status == 'results'){
        console.log("there are results");
        }

    },
    error: function(jqXHR, textStatus, errorThrown, data){
        console.log(jqXHR, textStatus, errorThrown, data);
    }
    });
    e.preventDefault();
});

这是PHP:

header('Content-type: application/json');

require_once '../../PHP/class.user.php';
$user_home = new USER();

$user_id = $_POST['user_id'];
$userPasswordConfirm = trim($_POST['userPasswordConfirm']);

$stmt = $user_home->runQuery("SELECT * FROM tbl_users WHERE user_id=:user_id LIMIT 1");
$stmt->execute(array(":user_id"=>$user_id));
$userRow = $stmt->fetch(PDO::FETCH_ASSOC);

if (empty($_SESSION['userSession'])) {
    $response_array['status'] = 'no_id';
    die();
}else{
    if ($userRow['userPass'] == hash('sha256', $userPasswordConfirm)){

        $user_profile = $user_home->runQuery("SELECT * FROM user_profiles WHERE user_id=:user_id LIMIT 1");
        $user_profile->execute(array(":user_id"=>$user_id));

        if($user_profile->rowCount() > 0) {   
            $response_array['status'] = 'results';
            if($userRow['userType'] == 'admin'){ 
            //DO NOTHING ATM

            }elseif($userRow['userType'] == 'family'){

                $userPassword = trim($_POST['userPassword']);
                $userRetypePassword = trim($_POST['userRetypePassword']);
                $userBirthCountry = trim($_POST['userBirthCountry']);
                $userBirthCity = trim($_POST['userBirthCity']);
                $userBirthAddress = trim($_POST['userBirthAddress']);
                $UserZipCode = trim($_POST['UserZipCode']);
                $userFirstName = trim($_POST['userFirstName']);
                $userLastName = trim($_POST['userLastName']);
                $userSex = trim(@$_POST['userSex']);
                $userBirthDay = trim($_POST['userBirthDay']);
                $userBirthMonth = trim($_POST['userBirthMonth']);
                $userBirthYear = trim($_POST['userBirthYear']);
                $userPhoneNr = trim($_POST['userPhoneNr']);
                $userDescription = 'puudub';
                $userQuote = 'puudub';

                $date = $userBirthYear . $userBirthMonth . $userBirthDay;

                $user_profile = $user_home->runQuery("UPDATE user_profiles SET aadress=:userBirthAddress, zip_code=:UserZipCode, city=:userBirthCity, country=:userBirthCountry, description=:userDescription, birthday=:date, quote=:userQuote, phone=:userPhoneNr WHERE user_id=:user_id ");
                $user_profile->bindparam(':userBirthAddress', $userBirthAddress, PDO::PARAM_STR);
                $user_profile->bindparam(':UserZipCode', $UserZipCode, PDO::PARAM_STR);
                $user_profile->bindparam(':userBirthCity', $userBirthCity, PDO::PARAM_STR);
                $user_profile->bindparam(':userBirthCountry', $userBirthCountry, PDO::PARAM_STR);
                $user_profile->bindparam(':date', $date, PDO::PARAM_STR);
                $user_profile->bindparam(':userDescription', $userDescription, PDO::PARAM_STR);
                $user_profile->bindparam(':userQuote', $userQuote, PDO::PARAM_STR);
                $user_profile->bindparam(':userPhoneNr', $userPhoneNr, PDO::PARAM_STR);
                $user_profile->bindparam(':user_id', $user_id, PDO::PARAM_STR);
                $user_profile->execute(); 
                return $user_profile;
                $response_array['status'] = 'success';
            }
        }else{
            $response_array['status'] = 'no_results';
            die();
        }
    }else{
        $response_array['status'] = 'error'; 
        die();
    }
echo json_encode($response_array);
}

修改

在控制台中获取此信息。 enter image description here

编辑2

来自网络标签的响应文字:

Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:294
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Cookie:ai_user=80979CD0-4B9F-4C5E-9735-0CBC582B6100|2015-06-18T09:54:30.192Z; ai_session=9B999B22-3969-4929-B47D-4B4DDDEF3E80|1434621270194|1434621694875; cookieconsent_dismissed=yes; PHPSESSID=ftqs9hdu5p1cc61s8o612uvep3; _ga=GA1.1.75323905.1449732737; loggedout=1
Host:localhost
Origin:http://localhost
Referer:http://localhost/Hoidja.ee/Dashboard/profile.php
User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36
X-Requested-With:XMLHttpRequest

编辑3

        }elseif($userRow['userType'] == 'pere'){
            $date = $userBirthYear . $userBirthMonth . $userBirthDay;

            $user_profile = $user_home->runQuery("UPDATE user_profiles SET aadress=:userBirthAddress, zip_code=:UserZipCode, city=:userBirthCity, country=:userBirthCountry, description=:userDescription, birthday=:date, quote=:userQuote, phone=:userPhoneNr WHERE user_id=:user_id ");
            $user_profile->bindparam(':userBirthAddress', $userBirthAddress, PDO::PARAM_STR);
            $user_profile->bindparam(':UserZipCode', $UserZipCode, PDO::PARAM_STR);
            $user_profile->bindparam(':userBirthCity', $userBirthCity, PDO::PARAM_STR);
            $user_profile->bindparam(':userBirthCountry', $userBirthCountry, PDO::PARAM_STR);
            $user_profile->bindparam(':date', $date, PDO::PARAM_STR);
            $user_profile->bindparam(':userDescription', $userDescription, PDO::PARAM_STR);
            $user_profile->bindparam(':userQuote', $userQuote, PDO::PARAM_STR);
            $user_profile->bindparam(':userPhoneNr', $userPhoneNr, PDO::PARAM_STR);
            $user_profile->bindparam(':user_id', $user_id, PDO::PARAM_STR);
            $user_profile->execute();
            ///I REMOVED THIS return $user_profile;
            $response_array['status'] = 'success';

我删除了退货后,它开始工作了。那是为什么?

1 个答案:

答案 0 :(得分:-1)

die之前您有多次echo json_encode($response_array);次来电。

因此,在调用die时,您永远不会重新启动JSON数据。

json_encode之前尝试die,它会更好。

die相当于exit,在调用时会终止脚本,因此在json_encode之后永远不会调用die

http://php.net/manual/en/function.exit.php

修改

另外,如果您看到截图,responseText为空,那么它不是JSON。

唯一没有定义的情况是if($userRow['userType'] == 'admin'){

纠正这种情况以返回一些东西。