How to get a JSON result in Ionic 3, but echo result in PHP

Asked

Viewed 261 times

1

I need to get the result from my JSON which could be "Registration already exists." or "Register Successfully." In my register, I am doing this way:

submit(){

    var nome = this.data.nome;
    var celular = this.data.celular;    
    var email = this.data.email;
    var senha = this.data.senha;


    var link = 'http://localhost/webapi/cadastro_usuario.php?email='+email+'&nome='+nome+'&senha='+senha+'&celular='+celular;
     //var myData = JSON.stringify({email: this.data.email}) + JSON.stringify({senha: this.data.senha});

    this.http.get(link)
     .subscribe(data => {
         this.data.response = data["_body"]; //https://stackoverflow.com/questions/39574305/property-body-does-not-exist-on-type-response

         console.log(this.data.response);

         if(JSON.stringify(this.data.response) === "Cadastro já existe."){

          const alert = this.alertCtrl.create({
            title: 'Usuário Já Cadastrado',
            subTitle: 'Este usuário já se encontra cadastrado. Caso tenha esquecido seu cadastro, vá em "Esqueci a Senha".',
            buttons: ['OK']
          });
          alert.present();
        }

        if(JSON.stringify(this.data.response) === "Cadastro Realizado com Sucesso."){

          const alert = this.alertCtrl.create({
            title: 'Bemvindo',
            subTitle: 'Obrigado por se cadastrar. Seja bem vindo ao WellFit App. Faça seu login.',
            buttons: ['OK']
          });
          alert.present();
        }
     })
     }

My Console.log returns this way:

<br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined index: telefone in C:\wamp64\www\webapi\cadastro_usuario.php on line <i>30</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0006</td><td bgcolor='#eeeeec' align='right'>370752</td><td bgcolor='#eeeeec'>{main}(  )</td><td title='C:\wamp64\www\webapi\cadastro_usuario.php' bgcolor='#eeeeec'>...\cadastro_usuario.php<b>:</b>0</td></tr>
</table></font>
Cadastro Realizado com Sucesso.

But in Postman returns normal, only messages.

How can I pick up messages without this mess?

To complete my PHP is this way:

<?php

include './classes/conexao.php';
include './classes/DAO/UsuarioDAO.php';

if (isset($_SERVER['HTTP_ORIGIN'])) {
    header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Max-Age: 86400');    // cache for 1 day
}

// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
    if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD'])) {
        header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
    }

    if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS'])) {
        header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
    }

    exit(0);
}

if ($_GET) {
    $email = $_GET['email'];
    $senha = $_GET['senha'];
    $nome = $_GET['nome'];
    $celular = $_GET['celular'];
    $telefone = $_GET['telefone'];

    $UsuarioDAO = new UsuarioDAO();

    $consulta = $UsuarioDAO->CadastraUsuario($email, $nome, $celular, $telefone, $email, $senha);

    if ($consulta == true) {
        echo 'Cadastro Realizado com Sucesso.';
    } else {
        echo 'Cadastro já existe.';
    }
}
  • 1

    try using Return instead of echo... return 'Cadastro Realizado com Sucesso.';

  • Same result.

  • 1

    I saw now that the proleme apparently has the phone variable, which should not be being passed. Try to do something like : $telefone = isset($_GET['telefone']) ? $_GET['telefone'] : null;

  • 1

    Our @Lucasbrogni was just that... lack of attention. Thank you, it was worth a lot!

  • 1

    I’ll put as an answer there, then just mark as solved the question.

1 answer

1


Apparently it is with the variable phone, which should not be being passed. Try to do something like :

$telefone = isset($_GET['telefone']) ? $_GET['telefone'] : null;

Browser other questions tagged

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