4
My code is like this:
sendAjax(new FormData(this), function(retorno) {
if(retorno.isArray)
{
$(".erro-login strong").text("ID: " + retorno.id + " usuário: " + retorno.usuario);
$(".erro-login").show("slow");
navigator.notification.beep(1);
}
else
{
$("input[name = usuario]").val(retorno);
$(".erro-login strong").text(retorno);
$(".erro-login").show("slow");
navigator.vibrate(1);
}
});
The variable retorno
may be of the String
or a Array JSON
, I want to know how I differentiate that in Jquey
? I tried to use the isArray
but it didn’t work, it keeps going into the else
with the return valid for example:
{
"tipo":1,
"usuario":"Guilherme",
"key":"66bd30f0cf4309c4ad7308fff5efffe8"
}
Briefly, how to differentiate a variable when it is JSON or String?
What ajax are you wearing? jQuery?
– Sergio
The backend return that is always text(string) would have to do a
retorno = JSON.parse(retorno);
to turn it into json 'for real'.– rray
Yes, the
ajax
is fromJquery
, I’ll try it here the way @rray quoted it.– Leonardo
@lvcs the accepted answer is a good tool but for another type of problem. In your case you use jQuery with
dataType: 'json'
as you said, then you always receive an Object and the code of the accepted answer always gives the same result(!)... that’s what you want?– Sergio
I tested all answers with and without dataType that in my case was indifferent since I could add or remove it without harming the rest of the code, and the only answer that managed to be functional was the one I marked, the others did not work with dataType or without it.
– Leonardo
And you tested if that method works
true
andfalse
? or always gives the same answer?– Sergio