2017-04-16 87 views
0

此PHP文件驗證用戶用電子郵件發送的ID和密碼。如果ID和代碼匹配確認消息,則顯示錯誤消息。該腳本轉換爲json,並在解析json轉換的php時返回錯誤。PHP JSON編碼解析錯誤

最後但並非最不重要創建在我使用來處理成功或錯誤消息,並在該命名的結果陣列被編碼以JSON末端的PHP的開始空數組。

這是錯誤:Uncaught SyntaxError: Unexpected end of JSON input

<?php 
require_once '../../dbconfig.php'; 

$result=[]; 

if(empty($_POST['verify_id']) && empty($_POST['code'])) 
{ 
$result['success']=false; 
$result['error_msg']="empty codes"; 
} 

if(isset($_POST['verify_id']) && isset($_POST['code'])) 
{ 
$id = base64_decode($_POST['verify_id']); 
$code = $_POST['code']; 

$statusY = "Y"; 
$statusN = "N"; 

$stmt = $db_con->prepare("SELECT user_id,userStatus FROM tbl_users WHERE user_id=:uID AND tokenCode=:code LIMIT 1"); 
$stmt->execute(array(":uID"=>$id,":code"=>$code)); 
$row=$stmt->fetch(PDO::FETCH_ASSOC); 
if($stmt->rowCount() > 0) 
{ 
    $result["success"]=true; 
    if($row['userStatus']==$statusN) 
    { 
    $stmt = $db_con->prepare("UPDATE tbl_users SET userStatus=:status WHERE user_id=:uID"); 
    $stmt->bindparam(":status",$statusY); 
    $stmt->bindparam(":uID",$id); 
    $stmt->execute(); 
    $msg = "ok "; 
    $result['success_msg']=$msg; 
    } 
    else 
    { 
    $result['success']=true; 
    $msg = "active "; 
    $result['success_msg']=$msg; 
    } 
} 
else 
{ 
    $result['success']=false; 
    $msg = "account not found "; 
     $result['id']=$id; 
    $result['error_msg']=$msg; 
} 
} 
echo json_encode($result); 
?> 

/*get data to activate account*/ 
 
var verifyResult=[]; 
 
var success_msg=document.getElementById("container"); 
 

 
window.onload=function(){ 
 
\t //sendDataToVerificationPage(); 
 
\t var url = window.location.href; 
 
    var myId = url.slice(77,79); 
 
\t var myCode= url.slice(87); 
 
\t sendDataToVerificationPage(myId,myCode); 
 
}; 
 

 
/*send data to verification page*/ 
 
function sendDataToVerificationPage(myId,myCode){ 
 
\t \t \t var oOutput = document.querySelectorAll("div"), 
 
\t \t \t \t oData = "verify_id="+myId+"&code="+myCode; 
 
\t \t \t var oReq = new XMLHttpRequest(); 
 
\t \t \t oReq.open("POST", "verify.php", true); 
 
\t \t \t oReq.onload = function(){ 
 
\t \t \t \t if(oReq.readyState==4 && oReq.status==200){ 
 
\t \t \t \t \t receivedData(oReq); 
 
\t \t \t \t } else{ 
 
\t \t \t \t \t oOutput.innerHTML = "error" + oReq.status + "occurred when trying to upload your file.<br\/>"; 
 
\t \t \t \t } 
 
\t \t \t }; 
 
\t \t \t oReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
 
\t \t \t oReq.send(oData); 
 
} 
 

 
/*callback function to handle the received data*/ 
 
function receivedData(oReq){ 
 
    console.log(oReq.responseText) 
 
\t verifyResult=JSON.parse(oReq.responseText); 
 
\t console.log(verifyResult); 
 
\t if(!verifyResult.success){ 
 
\t \t $("#container").fadeIn(1000, function(){  
 
\t \t $("#container").html('<div class="alert alert-danger"> <span class="glyphicon glyphicon-info-sign"></span> &nbsp; '+verifyResult.error_msg+'</div>'); 
 
     }); 
 
\t }else{ 
 
\t \t success_msg.textContent=verifyResult.success_msg; 
 
\t } 
 
}

+0

看來你解碼JSON字符串時有錯誤。你也可以展示那部分?我認爲這是JavaScript。 – Tchoupi

+0

@Tchoupi你是說我需要爲你提供js部分嗎? – Riccardo

+0

你需要使用它'JSON.parse(your_php_encoded_var)' –

回答

0

我已經通過更換成功和錯誤消息純文本解決了這個問題,其實不管我可以」 t以我試過的方式編碼爲json htlm標籤

$result['success']=true; 
 
    $msg = "ok"; 
 
    $result['success_msg']=$msg; 
 
    
 
    $result['success']=false; 
 
    $msg="account already activated"; 
 
    $result['error_msg']=$msg; 
 

 
    echo json_encode($result);