How to check if the return Ajax is empty

Asked

Viewed 43 times

-3

I’m trying to verify a Json feedback for a decision-making, but I’m not getting it, I have it:

if (sender.getFieldName() == 'IdUsuarioIntranet') {
    var MatriculaPessoa = sender.getValue();

    console.log("MATRÍCULA: " + MatriculaPessoa);

    $.getJSON(
        location.href, {
            IdPessoa: MatriculaPessoa
        },
        function(data) {

            if (data.CelularCorporativo == 'null') {
                console.log("VAZIO");
                data.CelularCorporativo = 'SEM CELULAR';
            }

            editors['CelularCorporativo'].setValue(data.CelularCorporativo);
            editors['EmailCorporativo'].setValue(data.EmailCorporativo);
        }
    )
}

To help the question, when I return to search for my user I have email, but I do not have mobile phone, the email is rescued correctly, but as I do not have Cellular registered the field Cellular Corportativo needs to be filled with a text.

inserir a descrição da imagem aqui

But it’s not working, some hint?

  • What exactly isn’t working?

  • Hello @Luizfelipe, the console doesn’t even show "EMPTY".

  • Is the AJAX request being made at least? What was the HTTP response obtained?

1 answer

1


The problem is why you’re trying to compare data.CelularCorporativo with null, but set null between quotes, and javascript will basically interpret this as a string, not as null.

The right thing would be

if (data.CelularCorporativo == null) {
    console.log("Vazio");
}

Or simply:

if (data.CelularCorporativo) {
    console.log("Vazio");
}

In the latter case any value that is not null (vaizo), false or 0 will be considered true.

Edit

This answer is valid if data.CelularCorporativo comes from the empty API and not with the null string'.

  • thanks, I really appreciate your tip.,

Browser other questions tagged

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