Php 如何在ZF2控制器中读取json请求?
我在前端使用Angular JS进行了开发,在后端使用Zend Framework 2进行了开发。我想使用Angular JS将一个json从前端发送到后端,并在Zend Framework 2中读取这个json 角度JSPhp 如何在ZF2控制器中读取json请求?,php,json,angularjs,zend-framework,Php,Json,Angularjs,Zend Framework,我在前端使用Angular JS进行了开发,在后端使用Zend Framework 2进行了开发。我想使用Angular JS将一个json从前端发送到后端,并在Zend Framework 2中读取这个json 角度JS self.updateUser = function(user){ var data = JSON.stringify( { id: $scope.user.id }
self.updateUser = function(user){
var data = JSON.stringify(
{
id: $scope.user.id
}
);
var config = {
headers: {
'Content-Type': 'application/json'
}
}
$http.post("/privado/usuario/updateuser", data, config)
.success(function(data, status, headers, config){
switch (data.result){
case 0: //OK
console.log("Result OK!!!");
break;
case 1: //No tiene acceso
console.log("Resultado NO ACCESS!!!");
break;
case 3: //Error de sistemas
console.log("Resultado KO!!!");
break;
}
})
.error(function(data){
console.log("Problems with the server!!!");
});
}
self.updateUser = function(user){
var data = $.param({
json: JSON.stringify({
id: $scope.user.id,
profiles: 2,
user: "John",
color: "blue"
})
});
var config = {
headers: {
'Content-Type': 'application/json'
}
}
$http.post("/privado/usuario/updateuser", data, config)
.success(function(data, status, headers, config){
switch (data.result){
case 0: //OK
console.log("Result OK!!!");
break;
case 1: //No tiene acceso
console.log("Resultado NO ACCESS!!!");
break;
case 3: //Error de sistemas
console.log("Resultado KO!!!");
break;
}
})
.error(function(data){
console.log("Problems with the server!!!");
});
}
self.updateUser = function(user){
var data = $.param({
id: $scope.user.id,
profiles: 2,
user: "John",
color: "blue"
});
$http.post("/privado/usuario/updateuser", data)
.success(function(data, status, headers, config){
switch (data.result){
case 0: //OK
console.log("Result OK!!!");
break;
case 1: //No tiene acceso
console.log("Resultado NO ACCESS!!!");
break;
case 3: //Error de sistemas
console.log("Resultado KO!!!");
break;
}
})
.error(function(data){
console.log("Problems with the server!!!");
});
}
在另一端,后端,在我的控制器中,我有下一个代码:
控制器
public function updateUserAction(){
$log = $this->getServiceLocator()->get("Zend/Log");
$request = $this->getRequest();
if ($request->isPost()){
$post_data = $request->getPost();
if ($post_data != null){
try{
$json = Zend\Json\Json::decode($post_data, Zend\Json\Json::TYPE_ARRAY);
return new JsonModel(array(
"result" => UsuarioController::RESULT_OK
));
}catch(\Exception $e){
//Error de acceso a BBDD
$log->info(get_class($this) . "::" . __FUNCTION__ . ": Se ha producido un error de acceso a BBDD o el elemento recibido no existe en BBDD. id: " + $post_data["id"]);
$log->info(get_class($this) . "::" . __FUNCTION__ . ": " . $e->getMessage());
return new JsonModel(array(
"result" => UsuarioController::RESULT_SYSTEM_ERROR
));
}
}else{
$log->info(get_class($this) . "::" . __FUNCTION__ . ": No se ha recibido información a través de post");
//Error al recibir los datos
return new JsonModel(array(
"result" => UsuarioController::RESULT_SYSTEM_ERROR
));
}
}
}
public function updateUserAction(){
$log = $this->getServiceLocator()->get("Zend/Log");
$request = $this->getRequest();
if ($request->isPost()){
$post_data = $request->getPost();
if ($post_data != null){
try{
$json = json_decode($post_data["data"]);
$log->info("Datos recibidos2: " . sizeof($json));
$log->info("id: " . $json["id"]);
return new JsonModel(array(
"result" => UsuarioController::RESULT_OK
));
}catch(\Exception $e){
//Error de acceso a BBDD
$log->info(get_class($this) . "::" . __FUNCTION__ . ": Se ha producido un error de acceso a BBDD o el elemento recibido no existe en BBDD. id: " + $post_data["id"]);
$log->info(get_class($this) . "::" . __FUNCTION__ . ": " . $e->getMessage());
return new JsonModel(array(
"result" => UsuarioController::RESULT_SYSTEM_ERROR
));
}
}else{
$log->info(get_class($this) . "::" . __FUNCTION__ . ": No se ha recibido información a través de post");
//Error al recibir los datos
return new JsonModel(array(
"result" => UsuarioController::RESULT_SYSTEM_ERROR
));
}
}
}
public function updateUserAction(){
$log = $this->getServiceLocator()->get("Zend/Log");
$request = $this->getRequest();
if ($request->isPost()){
$post_data = $request->getPost();
if ($post_data != null){
try{
$log->info("id: " . $_POST["id"]);
return new JsonModel(array(
"result" => UsuarioController::RESULT_OK
));
}catch(\Exception $e){
//Error de acceso a BBDD
$log->info(get_class($this) . "::" . __FUNCTION__ . ": Se ha producido un error de acceso a BBDD o el elemento recibido no existe en BBDD. id: " + $post_data["id"]);
$log->info(get_class($this) . "::" . __FUNCTION__ . ": " . $e->getMessage());
return new JsonModel(array(
"result" => UsuarioController::RESULT_SYSTEM_ERROR
));
}
}else{
$log->info(get_class($this) . "::" . __FUNCTION__ . ": No se ha recibido información a través de post");
//Error al recibir los datos
return new JsonModel(array(
"result" => UsuarioController::RESULT_SYSTEM_ERROR
));
}
}
}
如果我从后端删除这行代码:
$json = Zend\Json\Json::decode($post_data, Zend\Json\Json::TYPE_ARRAY);
代码运行良好。也就是说,前端进行调用并接收类似结果“0”的结果,并在控制台中写入“result OK!!!”。但如果我不删除那行代码,在前端我就不会收到任何结果,因为后端的某些东西不能正常工作
如何在控制器中读取从前端接收的数据
更新1
我添加了更多信息,可从前端发送到后端:
角度JS
self.updateUser = function(user){
var data = JSON.stringify(
{
id: $scope.user.id
}
);
var config = {
headers: {
'Content-Type': 'application/json'
}
}
$http.post("/privado/usuario/updateuser", data, config)
.success(function(data, status, headers, config){
switch (data.result){
case 0: //OK
console.log("Result OK!!!");
break;
case 1: //No tiene acceso
console.log("Resultado NO ACCESS!!!");
break;
case 3: //Error de sistemas
console.log("Resultado KO!!!");
break;
}
})
.error(function(data){
console.log("Problems with the server!!!");
});
}
self.updateUser = function(user){
var data = $.param({
json: JSON.stringify({
id: $scope.user.id,
profiles: 2,
user: "John",
color: "blue"
})
});
var config = {
headers: {
'Content-Type': 'application/json'
}
}
$http.post("/privado/usuario/updateuser", data, config)
.success(function(data, status, headers, config){
switch (data.result){
case 0: //OK
console.log("Result OK!!!");
break;
case 1: //No tiene acceso
console.log("Resultado NO ACCESS!!!");
break;
case 3: //Error de sistemas
console.log("Resultado KO!!!");
break;
}
})
.error(function(data){
console.log("Problems with the server!!!");
});
}
self.updateUser = function(user){
var data = $.param({
id: $scope.user.id,
profiles: 2,
user: "John",
color: "blue"
});
$http.post("/privado/usuario/updateuser", data)
.success(function(data, status, headers, config){
switch (data.result){
case 0: //OK
console.log("Result OK!!!");
break;
case 1: //No tiene acceso
console.log("Resultado NO ACCESS!!!");
break;
case 3: //Error de sistemas
console.log("Resultado KO!!!");
break;
}
})
.error(function(data){
console.log("Problems with the server!!!");
});
}
我也修改了控制器
控制器
public function updateUserAction(){
$log = $this->getServiceLocator()->get("Zend/Log");
$request = $this->getRequest();
if ($request->isPost()){
$post_data = $request->getPost();
if ($post_data != null){
try{
$json = Zend\Json\Json::decode($post_data, Zend\Json\Json::TYPE_ARRAY);
return new JsonModel(array(
"result" => UsuarioController::RESULT_OK
));
}catch(\Exception $e){
//Error de acceso a BBDD
$log->info(get_class($this) . "::" . __FUNCTION__ . ": Se ha producido un error de acceso a BBDD o el elemento recibido no existe en BBDD. id: " + $post_data["id"]);
$log->info(get_class($this) . "::" . __FUNCTION__ . ": " . $e->getMessage());
return new JsonModel(array(
"result" => UsuarioController::RESULT_SYSTEM_ERROR
));
}
}else{
$log->info(get_class($this) . "::" . __FUNCTION__ . ": No se ha recibido información a través de post");
//Error al recibir los datos
return new JsonModel(array(
"result" => UsuarioController::RESULT_SYSTEM_ERROR
));
}
}
}
public function updateUserAction(){
$log = $this->getServiceLocator()->get("Zend/Log");
$request = $this->getRequest();
if ($request->isPost()){
$post_data = $request->getPost();
if ($post_data != null){
try{
$json = json_decode($post_data["data"]);
$log->info("Datos recibidos2: " . sizeof($json));
$log->info("id: " . $json["id"]);
return new JsonModel(array(
"result" => UsuarioController::RESULT_OK
));
}catch(\Exception $e){
//Error de acceso a BBDD
$log->info(get_class($this) . "::" . __FUNCTION__ . ": Se ha producido un error de acceso a BBDD o el elemento recibido no existe en BBDD. id: " + $post_data["id"]);
$log->info(get_class($this) . "::" . __FUNCTION__ . ": " . $e->getMessage());
return new JsonModel(array(
"result" => UsuarioController::RESULT_SYSTEM_ERROR
));
}
}else{
$log->info(get_class($this) . "::" . __FUNCTION__ . ": No se ha recibido información a través de post");
//Error al recibir los datos
return new JsonModel(array(
"result" => UsuarioController::RESULT_SYSTEM_ERROR
));
}
}
}
public function updateUserAction(){
$log = $this->getServiceLocator()->get("Zend/Log");
$request = $this->getRequest();
if ($request->isPost()){
$post_data = $request->getPost();
if ($post_data != null){
try{
$log->info("id: " . $_POST["id"]);
return new JsonModel(array(
"result" => UsuarioController::RESULT_OK
));
}catch(\Exception $e){
//Error de acceso a BBDD
$log->info(get_class($this) . "::" . __FUNCTION__ . ": Se ha producido un error de acceso a BBDD o el elemento recibido no existe en BBDD. id: " + $post_data["id"]);
$log->info(get_class($this) . "::" . __FUNCTION__ . ": " . $e->getMessage());
return new JsonModel(array(
"result" => UsuarioController::RESULT_SYSTEM_ERROR
));
}
}else{
$log->info(get_class($this) . "::" . __FUNCTION__ . ": No se ha recibido información a través de post");
//Error al recibir los datos
return new JsonModel(array(
"result" => UsuarioController::RESULT_SYSTEM_ERROR
));
}
}
}
这些代码行:
$log->info("Datos recibidos2: " . sizeof($json));
$log->info("id: " . $json["id"]);
将下一个结果写入文件:
2015-11-19T18:23:30+01:00 INFO (6): Datos recibidos2: 0
2015-11-19T18:30:49+01:00 INFO (6): id:
我好像什么都没收到。。。如何获取接收到的数据
更新2
我已在前端修改了var数据,并从后端调用中删除了配置:
角度JS
self.updateUser = function(user){
var data = JSON.stringify(
{
id: $scope.user.id
}
);
var config = {
headers: {
'Content-Type': 'application/json'
}
}
$http.post("/privado/usuario/updateuser", data, config)
.success(function(data, status, headers, config){
switch (data.result){
case 0: //OK
console.log("Result OK!!!");
break;
case 1: //No tiene acceso
console.log("Resultado NO ACCESS!!!");
break;
case 3: //Error de sistemas
console.log("Resultado KO!!!");
break;
}
})
.error(function(data){
console.log("Problems with the server!!!");
});
}
self.updateUser = function(user){
var data = $.param({
json: JSON.stringify({
id: $scope.user.id,
profiles: 2,
user: "John",
color: "blue"
})
});
var config = {
headers: {
'Content-Type': 'application/json'
}
}
$http.post("/privado/usuario/updateuser", data, config)
.success(function(data, status, headers, config){
switch (data.result){
case 0: //OK
console.log("Result OK!!!");
break;
case 1: //No tiene acceso
console.log("Resultado NO ACCESS!!!");
break;
case 3: //Error de sistemas
console.log("Resultado KO!!!");
break;
}
})
.error(function(data){
console.log("Problems with the server!!!");
});
}
self.updateUser = function(user){
var data = $.param({
id: $scope.user.id,
profiles: 2,
user: "John",
color: "blue"
});
$http.post("/privado/usuario/updateuser", data)
.success(function(data, status, headers, config){
switch (data.result){
case 0: //OK
console.log("Result OK!!!");
break;
case 1: //No tiene acceso
console.log("Resultado NO ACCESS!!!");
break;
case 3: //Error de sistemas
console.log("Resultado KO!!!");
break;
}
})
.error(function(data){
console.log("Problems with the server!!!");
});
}
我也修改了控制器
控制器
public function updateUserAction(){
$log = $this->getServiceLocator()->get("Zend/Log");
$request = $this->getRequest();
if ($request->isPost()){
$post_data = $request->getPost();
if ($post_data != null){
try{
$json = Zend\Json\Json::decode($post_data, Zend\Json\Json::TYPE_ARRAY);
return new JsonModel(array(
"result" => UsuarioController::RESULT_OK
));
}catch(\Exception $e){
//Error de acceso a BBDD
$log->info(get_class($this) . "::" . __FUNCTION__ . ": Se ha producido un error de acceso a BBDD o el elemento recibido no existe en BBDD. id: " + $post_data["id"]);
$log->info(get_class($this) . "::" . __FUNCTION__ . ": " . $e->getMessage());
return new JsonModel(array(
"result" => UsuarioController::RESULT_SYSTEM_ERROR
));
}
}else{
$log->info(get_class($this) . "::" . __FUNCTION__ . ": No se ha recibido información a través de post");
//Error al recibir los datos
return new JsonModel(array(
"result" => UsuarioController::RESULT_SYSTEM_ERROR
));
}
}
}
public function updateUserAction(){
$log = $this->getServiceLocator()->get("Zend/Log");
$request = $this->getRequest();
if ($request->isPost()){
$post_data = $request->getPost();
if ($post_data != null){
try{
$json = json_decode($post_data["data"]);
$log->info("Datos recibidos2: " . sizeof($json));
$log->info("id: " . $json["id"]);
return new JsonModel(array(
"result" => UsuarioController::RESULT_OK
));
}catch(\Exception $e){
//Error de acceso a BBDD
$log->info(get_class($this) . "::" . __FUNCTION__ . ": Se ha producido un error de acceso a BBDD o el elemento recibido no existe en BBDD. id: " + $post_data["id"]);
$log->info(get_class($this) . "::" . __FUNCTION__ . ": " . $e->getMessage());
return new JsonModel(array(
"result" => UsuarioController::RESULT_SYSTEM_ERROR
));
}
}else{
$log->info(get_class($this) . "::" . __FUNCTION__ . ": No se ha recibido información a través de post");
//Error al recibir los datos
return new JsonModel(array(
"result" => UsuarioController::RESULT_SYSTEM_ERROR
));
}
}
}
public function updateUserAction(){
$log = $this->getServiceLocator()->get("Zend/Log");
$request = $this->getRequest();
if ($request->isPost()){
$post_data = $request->getPost();
if ($post_data != null){
try{
$log->info("id: " . $_POST["id"]);
return new JsonModel(array(
"result" => UsuarioController::RESULT_OK
));
}catch(\Exception $e){
//Error de acceso a BBDD
$log->info(get_class($this) . "::" . __FUNCTION__ . ": Se ha producido un error de acceso a BBDD o el elemento recibido no existe en BBDD. id: " + $post_data["id"]);
$log->info(get_class($this) . "::" . __FUNCTION__ . ": " . $e->getMessage());
return new JsonModel(array(
"result" => UsuarioController::RESULT_SYSTEM_ERROR
));
}
}else{
$log->info(get_class($this) . "::" . __FUNCTION__ . ": No se ha recibido información a través de post");
//Error al recibir los datos
return new JsonModel(array(
"result" => UsuarioController::RESULT_SYSTEM_ERROR
));
}
}
}
遗憾的是,这行代码没有返回任何内容
$log->info("id: " . $_POST["id"]);
2015-11-19T19:24:29+01:00 INFO (6): id:
我在console.log中发现了这个错误:
并且,在不更改前端任何内容的情况下,如果我更改控制器中的这行代码:
$log->info("id: " . $_POST["id"]);
另一方面:
$json = Zend\Json\Json::decode($post_data, Zend\Json\Json::TYPE_ARRAY);
并添加另一行以写入“id”的值:
它不起作用,因为我的行中有一个错误:
$json = Zend\Json\Json::decode($post_data, Zend\Json\Json::TYPE_ARRAY);
因为在我的文件里我什么都没写
$log->info("id: " . $_POST["id"]);
2015-11-19T19:24:29+01:00 INFO (6): id:
更新3
从我的上一个版本的代码中,如果我删除$.param var数据将是:
var data = {
id: $scope.user.id,
profiles: 2,
user: "John",
color: "blue"
};
它不工作,我在控制台中遇到了与以前相同的错误。我没有对控制器进行任何更改。您不需要使用:
headers: {
'Content-Type': 'application/json'
}
和JSON解码:
$json = Zend\Json\Json::decode($post_data, Zend\Json\Json::TYPE_ARRAY);
您可以像以前一样使用JSON发送数据(或者将JSON.stringify
函数更改为字符串格式:{data1:value1,data2:value2…}
),然后使用普通的$\u POST
(无JSON\u decode>)获取数据
因此,您可以使用JavaScript编写:
var data = {
id: $scope.user.id,
profiles: 2,
user: "John",
color: "blue"
};
在PHP中接收:
$_POST['id'], $_POST['profiles'], $_POST['user'] ....
也许这个主题会有帮助:谢谢Szymon,我已经修改了我的代码,但它不起作用:(谢谢Szymon,但我不知道我做错了什么,因为它对我不起作用:(尝试删除$.param,仅var数据={id:$scope.user.id,配置文件:2,用户:“John”,颜色:“blue”}。并检查另一个字段,而不是“id”(“user”可能),因此…..像前面一样添加此$.param,但请尝试:headers:{'Content-Type':'application/x-www-form-urlencoded'}(而不是json header).Php应该在更改标题后看到这一点。我之前已经检查过,它可以工作!!!但是如果我发送的json不起作用!!!我不明白:-(如果你想通过控制器发送json标题,你必须使用:$params=json\u decode(file\u get\u contents('s))php://input",对),;