Error in php data return in Ionic

Asked

Viewed 216 times

4

I’m making an Ionic app in which I place the zip in a field and the backend returns me all other data such as state, city, street... But something strange happens, it returns the whole structure of php and not the data that should come. I made a test application, on the web, that works, only on Ionic that is giving this problem.

Follow the console error: print

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:

inserir a descrição da imagem aqui

  • Post the code of how you communicate with the backend

  • How’s your code? Put the PHP that returns these data.

  • The file extension is . PHP?

  • Apparently PHP was not executed, raw was sent from source.

2 answers

1


Put it like this instead 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 you don’t have a Webserver to compile it. In the case of the way you did, you were accessing the . php file directly.

Hugs!

  • If I do not enter " " at http://localhost:8888/systems/system_web/Ionic/vcApp/www/php/pegaCep.php? cep=" dai does not work

  • Oops, my mistake! should put the link between ""

0

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

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 the include check and see if there is a function json_encode on the server. Do so:

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

die();

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


In the code of Angular try like 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);
});
  • Continues the problem...

  • Put Return on all q? On all die()?

  • Nothing, the problem continues.

  • I believe in the 1st. because I pass the zip code.

  • Says json_encode exists

  • Fine. I changed my code in the POST, see now. I added the header. Notice I changed the final code on ifs.

  • Nothing, still shown all php

  • I put in my post a print of the console’s Network tab.

  • I changed the angular code, see.

  • Now you’re giving error in the app and not in php

  • What mistake you’re making ?

  • Open up all disfigured

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.