JSON message does not appear

Asked

Viewed 511 times

2

I have this code in PHP:

if($qryInsert == 1){
        //echo "Evento criado com sucesso!";
        echo json_encode( array('status' => 1, 'msg' => 'Evento criado com sucesso!')); 
    }else{
        //echo "Erro ao criar evento";
        echo json_encode( array('status' => 0, 'msg' => 'Erro ao criar evento')); 
    }

in my JS, it’s like this:

$.ajax({
    url: 'php/evento.php',
    type: 'POST',
    dataType: 'json',
    data: dataString,
    success: function(data) {
        if (data.status === '1') {
            $("#msg").val(data.status);
            $("#msg").show();
        }
    }
});

my html...

<div id="msg"></div>

I believe the error is in JS, but where?

  • 1

    You got the dataType closed to JSON? have you tested with number 1 (no quotes) and no string here: data.status === '1'? what gives console.log(data);?

  • $.ajax({ url: 'php/event.php', type: 'POST', dataType: 'json', date: dataString, }

  • Okay, the dataType all right. forehead if(data.status == 1){ without quotation marks and if it doesn’t work, say what happens success: function(data){ alert(JSON.stringify(data));}?

  • I tried and nothing appeared on the console

  • I will try this your Alert(JSON.stringify(date));

  • Nor did the message from Alert appear

  • Where should I put Alert(...)? Before or after IF?

  • Before the if, but it’s the same as console.log(data)... See what appears in Browser Tools in the "Network" section... and join in jQuery .fail(function(e) {&#xA; alert(e);&#xA; }) see if it gives you more info.

  • Where and how, exactly, I use the . fail(Function(e){Alert(e):})?

  • Success: Function(data){ Alert(JSON.stringify(data)); if(date.status == 1){ $("#msg"). val(data.status); $("#msg"). show(); } }

  • Regarding the use of JSON.stringfy, this would parse an object in string, and to parse it again for object it would be necessary to use JSON.parse. The return that PHP generates is in string, so it does if necessary to use JSON.parse

  • verify that the file that prints the json is "UTF8 without good" (ANSI-UTF8). If you have the character "GOOD (byte order mark)", remove it.

Show 8 more comments

2 answers

2

You need to parse the answer for JSON to be able to access the attributes of your object.

success: function(data){

            var resposta = JSON.parse(data);          

            if(resposta.status === '1'){
               $("#msg").val(resposta.status);
               $("#msg").show();
            }
         }
  • Apparently console.log(data); gives nothing, then the problem is before...

  • Hi guys, I still need some help on my JS that isn’t showing php: $. ajax({ url: 'php/event.php', type: 'POST', dataType: 'json', date: dataString, Success: Function(data){ Alert(JSON.stringify(data); if(data.status == 1){ $("#msg"). val(date.status); $("#msg"). show(); } }, error: Function (data) { console.log(data); } });

  • if($qryInsert == 1){ //echo "Event successfully created!" ; echo json_encode( array('status' => 1, 'msg' => 'Event successfully created!')); }Else{ //echo "Error creating event"; echo json_encode( array('status' => 0, 'msg' => 'Error creating event'); }

  • Post your PHP and JS in a fiddle please

  • Hi @Fabricio. What part of the fiddle do I put php? http://jsfiddle.net/gugasevero/f5f7w9vr/

  • @Gustavosevero: http://phpfiddle.org/

Show 1 more comment

0

Look at,

resposta.status // Não vai te trazer o conteúdo da resposta, apenas seu status.
resposta.responseText //seria o ideal.

One other little thing,

$('#msg').html(resposta.responseText)// provavelmente seria mais apropriado pro seu div.
//Já que .val() é para preencher o atributo value=""

Browser other questions tagged

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