0

I'm using $http.get to get some information from the server. First the controller calls the BackendServices, and in the service i call $http.get:

Controller:

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);
    };
}]);

Service:

app.service('BackendServices', function ($http) {
    var backendServices = {};

    backendServices.lookForCourses = function () {
        return $http.get('app/backend/lookForCourses');
    }

    return backendServices;
});

The PHP files works under cakePHP framework.

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);
}

Doing this i get as a response on the console:

Object{data: "", status: 200, config: Object, statusText: "OK"}

If I do this:

var_dump($cursos);

I get the following:

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)

However, if i do the following:

$test = array(array('nombre' => 'Propedeutico'), array('nombre' => 'Tecnologias'));

echo json_encode($test);

I do get that array as a response...

What am I missing? I know this might be a silly mistake, but I haven't been able to solve it so far...

Thanks a lot!!

2
  • How about echo json_encode($cursos, true)? Commented Jul 15, 2016 at 15:23
  • Same result on the response like that... Object{data: "", status: 200, config: Object, statusText: "OK"} Commented Jul 15, 2016 at 15:25

1 Answer 1

1

I made it work doing a little modification, since the result of the request brings back a string with accents, example: "Tecnología", i had to utf8_encode each one of the elements like this:

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'] = utf8_encode($curso['Curso']['nombreCurso']);
    }   

    echo json_encode($cursos);  
}

Adding ut8_encode did the trick.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.