PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, January 25, 2022

[FIXED] AngularJs - json_encode returning nothing in certain cases

 January 25, 2022     angularjs, cakephp, json, response     No comments   

Issue

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!!


Solution

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.



Answered By - Fer Vargas
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home
View mobile version

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing