No return Json to Ajax

0

I do not get Json's return from Ajax. Any idea what that might be?

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;

insere.php

$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);insira o código aqui

Full Code

    
asked by anonymous 12.05.2016 / 21:10

3 answers

0

Looking at the code quickly I saw something that if it is present in the code that you are running may actually be the cause. It's the last line:

echo json_encode($arr);insira o código aqui

delete after;

If this is not present in the code do so:

1st change the code for this:

alert('antes ajax'); //ou console.log, como preferir
$.ajax({
    type: 'POST',
    dataType: 'json',
    url: "crud/insere.php",
    data: dados,
    success: function(data) {
        alert('funcao success');
        var objeto = JSON.parse(data);
        alert(objeto.id);

     }
 });
 return false;

to see if there is any syntax error or if the code does not even run. If you know how to use it, you'd better use the browser debugger instead of the alerts or console.log.

Then check the apache log or your http server. For apache it's usually in /var/log/apache/error.log. See if you have any php errors there.

After this, in case you do not use some debugger with php to see where it is failing, put some echos in several parts of the code even if it looks bad on the screen to see where the error line occurs.

If this is not enough to see where the error is, when you find the line, edit the question with the part of the code where the error is and I will continue to help you.

    
12.05.2016 / 21:43
0

In this situation just look at what the ajax request is returning. In the page that makes the request go to inspect elements, network and see what is happening with your request (will vary from browser to browser). So you will see if the error is in the request, the server, or the client.

    
12.05.2016 / 23:20
0

Just one more detail. When you enter dataType: 'json', the callback success parameter is already a json object, that is, JSON.parse is unnecessary and may generate an exception in some cases.

Your JS code looks like this:

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

});

In your PHP file it is indispensable to include the json header:

<?PHP
$data = /** seus dados **/;
header('Content-Type: application/json');
echo json_encode($data);

Another important thing is that json_encode () only works with data in UTF-8. That is, if your php file or your data in the database is in ISO-8859-1, you must first make utf8_encode () in each of the strings.

    
03.12.2016 / 21:05