0
我使用$ http.get从服务器获取一些信息。首先,控制器调用BackendServices,并在服务我调用$ http.get:AngularJs - json_encode在某些情况下什么也不返回
控制器:
app.controller('courseController', ['$scope', 'BackendServices', function ($scope, BackendServices) {
BackendServices.lookForCourses().then(
function (response) {
console.log(response);
},
function (response) {
}
);
$scope.addCourse = function (courseName) {
console.log(courseName);
};
}]);
服务:
app.service('BackendServices', function ($http) {
var backendServices = {};
backendServices.lookForCourses = function() {
return $http.get('app/backend/lookForCourses');
}
return backendServices;
});
PHP文件CakePHP的工作框架下。
lookForCourses:
public function lookForCourses() {
$this->autoRender = false;
$cursosFind = $this->Curso->find('all', array('fields' => array('nombreCurso')));
$cursos = array();
foreach($cursosFind as $index => $curso) {
$cursos[$index]['nombre'] = $curso['Curso']['nombreCurso'];
}
echo json_encode($cursos);
}
这样做,我得到的控制台上的一个回应:
对象{数据: “” 状态:200,配置:对象,状态文本: “OK”}
如果我这样做:
var_dump($cursos);
我得到如下:
array (size=3)
0 =>
array (size=1)
'nombre' => string 'Tecnologias de la informacion' (length=29)
1 =>
array (size=1)
'nombre' => string 'Propedeutico' (length=12)
2 =>
array (size=1)
'nombre' => string 'Lectura y redaccion' (length=19)
但是,如果我做到以下几点:
$test = array(array('nombre' => 'Propedeutico'), array('nombre' => 'Tecnologias'));
echo json_encode($test);
我得到这个数组作为响应...
我缺少什么?我知道这可能是一个愚蠢的错误,但我迄今还没有能够解决它...
非常感谢!
'echo json_encode($ cursos,true)'? – Ankh
同样的结果就像这样... Object {data:“”,status:200,config:Object,statusText:“OK”} –