I don’t get Json return in Ajax with PHP

Asked

Viewed 76 times

0

can’t get out of place, what’s wrong? I don’t get the feedback from Json in Ajax.

register Customer.php

Ajax.js

$.ajax({
    type: 'POST',
    dataType: 'json',
    url: "crud/insere.php",
    data: dados,
    success: function(data) {

        var objeto = JSON.parse(data);
        alert(objeto.id);

     }
});
return false;

php insert.

$nome = $_POST['nome'];
$telefone1 = $_POST['telefone1'];
$operadora1 = $_POST['operadora1'];
$telefone2 = $_POST['telefone2'];
$operadora2 = $_POST['operadora2'];
$email = $_POST['email'];
$cidade = $_POST['idCidade'];
$observacao = $_POST['obs'];
$dataCadastro = date('Y-m-d H:i:s');

if(empty($cidade)){
    $cidade = '0';
}

$select = "INSERT INTO Cliente(nome, telefone1, telefone2, operadora1,  operadora2, 
           email, idCidade, observacao, dataCadastro) 
           VALUES ('$nome', '$telefone1', '$telefone2', '$operadora1', '$operadora2', '$email', '$cidade','$observacao', '$dataCadastro')";
$conexao = conexao();           
$PDO=$conexao->prepare($select);
$PDO->execute();

$select = "SELECT id FROM Cliente WHERE email='$email'";
$PDO=$conexao->prepare($select);
$PDO->execute();
$obj = $PDO -> fetch(PDO::FETCH_OBJ);

$arr = array('id' => $obj->id);
echo json_encode($arr);

Complete source code inserir a descrição da imagem aqui

  • This line is not needed because you already said it is type:json, var objeto = JSON.parse(data);. In terms of database security entering the values like this is very insecure, but maybe you’re just testing.

  • Even so, nothing happens, not even in the browser console.

  • Dude, if you have an example that works and you can send it to me,.

1 answer

0

Dude, I still have to declare the header.

In the penultimate line of your php file puts this :

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

Browser other questions tagged

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