大家好,我在php中有离子和后端的前端。现在我试图将一些数据发送回我的角度应用程序,但我遇到了一些错误。我试图找到解决方案,但我找不到解决方案。请帮我解决这个问题谢谢。我得到的错误是:
SyntaxError: Unexpected token { in JSON at position 331
at Object.parse (native)
at fromJson (http://192.168.10.5:8100/lib/ionic/js/ionic.bundle.js:14655:14)
at defaultHttpResponseTransform (http://192.168.10.5:8100/lib/ionic/js/ionic.bundle.js:23675:16)
at http://192.168.10.5:8100/lib/ionic/js/ionic.bundle.js:23766:12
at forEach (http://192.168.10.5:8100/lib/ionic/js/ionic.bundle.js:13691:20)
at transformData (http://192.168.10.5:8100/lib/ionic/js/ionic.bundle.js:23765:3)
at transformResponse (http://192.168.10.5:8100/lib/ionic/js/ionic.bundle.js:24554:21)
at processQueue (http://192.168.10.5:8100/lib/ionic/js/ionic.bundle.js:29127:28)
at http://192.168.10.5:8100/lib/ionic/js/ionic.bundle.js:29143:27
at Scope.$eval (http://192.168.10.5:8100/lib/ionic/js/ionic.bundle.js:30395:28)
我们关注的部分是:
if($request->category == "key"){
error_log("in key");
$key_action = $this->Battleplan_Util->getKeyBattleplan($param["for"]);
$key_action_count = count($key_action);
if ($key_action_count == 0){
error_log("adding");
$this->addActionInBattleplan($param);
}else{
error_log("error found in key");
$data["status"] = "false";
$data["type"] = "key";
$data["data"] = $key_action;
error_log("Data is : ". print_r($data , true));
// echo json_encode(array("status"=>"false","type"=>"key","data"=>$key_action[0]));
echo json_encode(array("data" => $data));
}
我的php控制器功能,我将数据发送回我的角度视图是这样的:
public function addBattleplan(){
// error_log("Hwllo world");
// die;
// $param = $this->input->post();
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$token = $request->token;
$referrer = $request->referrer;
// $type = $request->type;
$param["plan"] = $request->plan;
$param["date"] = $request->date;
$param["category"] = $request->category;
$token = $this->common_util->Decrypt('XXXXXXX',$token);
$user = $this->User_Util->get_token($token);
if (!$user) {
echo false;
}
$user_id = $user[0]['user_id'];
if ($request->target) {
$param['target'] = date('M d, Y' , strtotime($request->target));
} else {
$param['target'] = '--';
}
// $param['date'] = $this->getToday();
$param['date'] = date('M d, Y');
$param['starred'] = FALSE;
$param['archived'] = FALSE;
$param["for"] = new MongoId($user_id);
$param["type"] = "daily";
error_log("The category is: ". $this->category);
if ($request->category == "additional"){
error_log("in additional");
$this->addActionInBattleplan($param);
}
else{
if($request->category == "key"){
error_log("in key");
$key_action = $this->Battleplan_Util->getKeyBattleplan($param["for"]);
$key_action_count = count($key_action);
if ($key_action_count == 0){
error_log("adding");
$this->addActionInBattleplan($param);
}else{
error_log("error found in key");
$data["status"] = "false";
$data["type"] = "key";
$data["data"] = $key_action;
error_log("Data is : ". print_r($data , true));
// echo json_encode(array("status"=>"false","type"=>"key","data"=>$key_action[0]));
echo json_encode(array("data" => $data));
}
}else{
error_log("in important");
$imp_action = $this->Battleplan_Util->getImportantBattleplan($param["for"]);
$imp_actions_count = count($imp_action);
if($imp_actions_count < 5){
$this->addActionInBattleplan($param);
}else{
echo json_encode(array("status"=>"false" , "type"=> "important" , "data"=>$imp_action));
}
}
}
echo json_encode(array("status" => "true"));
return;
// $id = $this->Battleplan_Util->addBattleplan($param);
// if($id)
// {
// // $c_id = $this->calendar_util->insertCalendar($param['date'] , $param['target'], $param['plan'], '', 'actions', $id);
// // $this->battleplan_util->updateBattleplaCalendarId($id,$c_id);
// }
// echo 'true';
// return;
// }
}
这是我试图发送数据的离子代码
var request = $http({
method: "post",
url: link,
data: {
token:token,
plan: action,
target: date,
category: type,
},
});
request.success(function (data) {
debugger;
$scope.tittle = data[0]['plan'];
$scope.created =data[0]['date'];
$scope.targetD = data[0]['target'];
var alertPopup = $ionicPopup.alert({
title: 'Data Sent And Received Successfully',
template: data
});
alertPopup.then(function(res) {
console.log('Data Sent And Received Successfully');
$scope.checkStatus=false;
});
});
请告诉我,我做错了什么
** JSON **
我正在尝试将此数据编码为json。这是:
[23-May-2016 11:53:38 Asia/Karachi] Data is : Array
(
[status] => false
[type] => key
[data] => Array
(
[0] => Array
(
[_id] => MongoId Object
(
[$id] => XXXXXXXXXXXXXXXXXX
)
[plan] => Action
[target] => May 20, 2016
[status] => active
[type] => daily
[for] => MongoId Object
(
[$id] => XXXXXXXXXXXXXX
)
[category] => key
[date] => May 20, 2016
[starred] =>
[archived] =>
[visibility] => Array
(
[0] => MongoId Object
(
[$id] => XXXXXXXXXXXXXXXXX
)
)
)
)
)
已加星标和已归档的内容中包含错误但未在错误日志中显示
答案 0 :(得分:3)
修改您的控制器代码,如下所示。
public function addBattleplan(){
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$token = $request->token;
$referrer = $request->referrer;
// $type = $request->type;
$param["plan"] = $request->plan;
$param["date"] = $request->date;
$param["category"] = $request->category;
$token = $this->common_util->Decrypt('XXXXXXX',$token);
$user = $this->User_Util->get_token($token);
if (!$user) {
echo false;
return;
}
$user_id = $user[0]['user_id'];
if ($request->target) {
$param['target'] = date('M d, Y' , strtotime($request->target));
} else {
$param['target'] = '--';
}
// $param['date'] = $this->getToday();
$param['date'] = date('M d, Y');
$param['starred'] = FALSE;
$param['archived'] = FALSE;
$param["for"] = new MongoId($user_id);
$param["type"] = "daily";
error_log("The category is: ". $this->category);
if ($request->category == "additional"){
error_log("in additional");
$this->addActionInBattleplan($param);
}
else{
if($request->category == "key"){
error_log("in key");
$key_action = $this->Battleplan_Util->getKeyBattleplan($param["for"]);
$key_action_count = count($key_action);
if ($key_action_count == 0){
error_log("adding");
$this->addActionInBattleplan($param);
}else{
error_log("error found in key");
$data["status"] = "false";
$data["type"] = "key";
$data["data"] = $key_action;
error_log("Data is : ". print_r($data , true));
// echo json_encode(array("status"=>"false","type"=>"key","data"=>$key_action[0]));
$rawdata=json_encode(array("data" => $data));
}
}else{
error_log("in important");
$imp_action = $this->Battleplan_Util->getImportantBattleplan($param["for"]);
$imp_actions_count = count($imp_action);
if($imp_actions_count < 5){
$this->addActionInBattleplan($param);
}else{
$rawdata= json_encode(array("status"=>"false" , "type"=> "important" , "data"=>$imp_action));
}
}
}
echo $rawdata;
return;
}
希望这会对你有所帮助。