php json编码解析错误

时间:2017-04-16 13:53:33

标签: php json pdo

此php文件验证用户通过电子邮件发送的ID和代码。如果id和代码匹配,则显示确认消息,否则显示错误消息。脚本转换为json,并在解析json转换的php时返回错误。

最后但并非最不重要的是我在php的开头创建了一个空数组,用于处理成功或错误消息,最后将名为result的数组编码为json。

这是错误: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(){
	//sendDataToVerificationPage();
	var url = window.location.href;
    var myId = url.slice(77,79);
	var myCode= url.slice(87);
	sendDataToVerificationPage(myId,myCode);
};

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

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

1 个答案:

答案 0 :(得分:0)

我已经通过用纯文本替换成功和错误消息来修复问题,事实上我无法以我尝试的方式编码为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);