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?
You got the
dataType
closed toJSON
? have you tested with number1
(no quotes) and no string here:data.status === '1'
? what givesconsole.log(data);
?– Sergio
$.ajax({ url: 'php/event.php', type: 'POST', dataType: 'json', date: dataString, }
– GustavoSevero
Okay, the
dataType
all right. foreheadif(data.status == 1){
without quotation marks and if it doesn’t work, say what happenssuccess: function(data){ alert(JSON.stringify(data));}
?– Sergio
I tried and nothing appeared on the console
– GustavoSevero
I will try this your Alert(JSON.stringify(date));
– GustavoSevero
Nor did the message from Alert appear
– GustavoSevero
Where should I put Alert(...)? Before or after IF?
– GustavoSevero
Before the
if
, but it’s the same asconsole.log(data)
... See what appears in Browser Tools in the "Network" section... and join in jQuery.fail(function(e) {
 alert(e);
 })
see if it gives you more info.– Sergio
Where and how, exactly, I use the . fail(Function(e){Alert(e):})?
– GustavoSevero
Success: Function(data){ Alert(JSON.stringify(data)); if(date.status == 1){ $("#msg"). val(data.status); $("#msg"). show(); } }
– GustavoSevero
Let’s go continue this discussion in chat.
– Sergio
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
– user20212
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.
– Daniel Omine