Good evening,
If you specify in the option object sent to method .ajax
to key dataType
, you inform what type of data you are expecting from the server. In your case, it would look something like this:
jQuery.ajax({
/* ... */
dataType: 'json',
/*... */
})
However , as default, jQuery tries to guess - Intelligent Guess -, the format of the received data (xml, script , json ...) from the server response. So this above parameter would only need to be informed if jQuery could not understand that the response is a JSON (usually due to a badly formatted message by the server), or you want to make sure the result is always the one (in this case , if jQuery can not parse, it will invoke the callback error
).
That said, then your callback function success
will be invoked with the first parameter containing the formatted data parsed ) (in your case, an object JavaScript). And you can manipulate it as if you were manipulating a JavaScript object normally. Something like this:
jQuery.ajax({
/* ... */
success: function(data, textStatus, jqXHR )
{
/* seu dado já deve estar no parâmetro data */
console.log("Login com sucesso? ", !data.error);
if (!data.error) {
var user = data.user[0]; //seu user é um array, então pego o primeiro elemento dessa array, por exemplo
console.log("Nome completo do usuário: ", user.fullname);
}
}
});
The above code is for debug purposes only, because it will write to the browser console.
I do not know if this was your question (on how to manipulate the data received).
If you want to display a simple message, you can call the alert
. This will show a dialog box with the (very simple) message.
Or, if you want to insert this message into the body of the page in some way, or make element interactions, this involves manipulations in the DOM, and can be easily done by jQuery
. A very simple example:
jQuery.ajax({
/* ... */
success: function(data, textStatus, jqXHR )
{
var textMessage;
if (data.error) {
textMessage = "Ocorreu um erro ao relizar o login: " + data.message;
} else {
textMessage = "Login realizado com sucesso! Bem vindo, " + data.user[0].fullname + "!";
}
$("body").append( $("<p></p>", { text: textMessage }) );
}
});
The above example only adds to <body>
a <p>
(paragraph) with a message.