2016-05-19 12 views
-2

2日間の読書と検索で、他のすべての回答と質問を見てみましたが、わかりません。たぶん私は自分のコードで何か小さなものを見逃しています。JSON SyntaxError:JSON入力の予期しない終了(...)undefined

私はJSON SyntaxError: Unexpected end of JSON input(…) undefinedを取得しています。私は{またはA」を行方不明のように、これはおそらくどこか悪いJSONに関連しているけど、私はそれを見ることができない

ここに私の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); 
} 

EDIT

コンソールでこれを取得する。 enter image description here

ネットワーク]タブから

EDIT 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 

EDIT 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'; 

私はリターンを取り除いた後、それが仕事を始めました。何故ですか?

+0

JSON出力の例を質問に追加してください。 –

+0

jsonが見えますか? –

+0

バリデーターを使ってJSONを実行して、有効かどうかを確認できます。http://jsonlint.com/ –

答えて

-1

echo json_encode($response_array);の前に複数のdieコールがあります。

dieが呼び出されたときにJSONデータを再実行することはありません。

json_encodeに、dieより前にお試しください。

dieと呼ばれる、あなたを見れば、そうjson_encodeは、またdie

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


EDIT

後に呼び出されることはありませんスクリプトを終了exitに相当しますscreenshot、responseTextは空ですので、JSONではありません。

何も定義しない場合は、if($userRow['userType'] == 'admin'){です。

何かを返す場合は、このケースを修正してください。

+0

テストのためにdie()をすべて削除しましたが、エラーは引き続き発生します。 – raqulka

+0

私は答えを更新しました。 –

+0

私はそこにno_dataというレスポンスを追加しましたが、まだ何もありません。 – raqulka

関連する問題