Error in php data return on ionic

4

I'm doing an ionic app in which I put the zip in a field and the backend returns all other data, such as state, city, street ... But something strange happens, it returns the entire structure of php and not the data that should come. I did a test application, on the web, that works, only on the ionic that is giving this problem.

The error in the console follows:

angular:

angular.module('app.controllers',[]).controller('enderecoCtrl',function($scope,$http){$scope.pegaCep=function(){//console.log($scope.endereco.cep);$http.get("php/pegaCep.php?cep="+$scope.endereco.cep).then(function (endereco){
        console.log(endereco);

        });
     }
})

php:

<?php

include('correios.class.php');

$cep = $_GET['cep'];

if(isset($_GET['cep'])){
    $correios = Correios::cep($_GET['cep']);
    $correios = json_encode($correios[0]);
        die($correios);
}elseif(isset($_GET['codigo_rastreio'])){
    die(json_encode(Correios::rastreio($_GET['codigo_rastreio'])));
}else{
    die('informe parametro GET cep ou codigo_rastreio');
}

?>

Print Console Network Tab:

    
asked by anonymous 26.01.2016 / 12:24

2 answers

1

So put in the place of $ http.get

 $http.get("http://localhost:8888/sistemas/sistemas_web/ionic/vcApp/www/php/pegaCep.php?cep="+$scope.endereco.cep)

The way you did, PHP is not being compiled because it does not have a WebServer to compile it. In the case of the way you did, you were accessing the .php file directly.

Hugs!

    
26.01.2016 / 13:51
0

Takes die() as a function return and uses return itself.

if(isset($_GET['cep'])){
    $correios = Correios::cep($_GET['cep']);
    $correios = json_encode($correios[0]);
}elseif(isset($_GET['codigo_rastreio'])){
    $dados = Correios::rastreio($_GET['codigo_rastreio']);
    $correios = json_encode($dados);
}else{
   $correios = 'informe parametro GET cep ou codigo_rastreio';
}

header('Content-Type: application/json');
return $correios;

Under include perform a check and see if the json_encode function exists on the server. Here's how:

if (function_exists('json_encode')) {
    echo 'Existe';
} else {
    echo 'Não Existe';
}

die();

See what returns on the Network > XHR tab in Inspect Element.

In code of Angular try this:

$http.get($http({method: 'GET', url: "php/pegaCep.php?cep="+$scope.endereco.cep,  headers: {'Content-type': 'application/json'}}).success(function(endereco){
    console.log(endereco);
});
    
26.01.2016 / 12:32