5
The problem
I’m making a requisition Ajax to return some data from a table. He arrives and enters this function to start the foreach of the information, but two of them are getting "lost" on the way. I gave a warning on value['outras_atividades'] before he entered the if, and he showed the text of that field normally, but went inside the if that he became like undefined, that is, I cannot fill the text fields with these values.
My Code
Ajax
$('#verificaEntidade').click(function () {
        $.ajax({
            type: "POST",
            dataType: "json",
            url : "retornaEntidade",
            data: $('#formVerifEnt').serialize(),
            success : function(msg){
                if(msg.status == 0) {
                    alert("O formulário dessa entidade ainda não foi preenchido !");
                }
                else {
                    msg.errorMsg.forEach(ShowResults);
                }
            },
            error:function (xhr, ajaxOptions, thrownError) {
                 alert("Erro no Processamento dos Dados. Entre em contato com o setor de Tecnologia e informe a mensagem abaixo:\n"+xhr.responseText);
            }
        });
    });
Function to mark form fields
function ShowResults(value, index, ar) {
        var i = 0;
        /*Faz a verificação para atualizar os checkboxes das atividades realizadas*/
        if(value['codigo_atividade']) {
            for(i = 1; i <= 11; i++) {
                if(value['codigo_atividade'] == i) {
                    $('#atv'+ i).prop('checked', true);
                    if(value['codigo_atividade'] == 11) {
                        $('input[name=txtOutrasAtividades]').val(value['outras_atividades']);
                    }
                }
            }
        }
        /*Faz a verificação para atualizar os checkboxes dos públicos atendidos*/
        if(value['codigo_publico']) {
            for(i = 1; i <= 9; i++) {
                if(value['codigo_publico'] == i) {
                    $('#pub'+ i).prop('checked', true);
                    if(value['codigo_publico'] == 9) {
                        $('input[name=txtOutrosPublicos]').val(value['outros_publicos']);
                    }
                }
            }
        }
    }
What gives
alert(typeof value['outras_atividades']);?– Sergio
Have you thought about adding a variable, then if calling the variable? value = value['codigo_activity']
– Vanderson Ramos
@Vanderson I’ve done it to make a test, and the result was the same.
– Allan Ramos
Have you tried jQuery.proxy?? http://blog.dmatoso.com/2011/05/howto function/
– Vanderson Ramos
@Allanramos gave
undefinedbefore theif? or be right aftervar i = 0;?– Sergio
@Sergio did Alert right after var i = 0; and he gave string, and inside if gave Undefined
– Allan Ramos
Okay. That mistake doesn’t make much sense to me, the
ifdoesn’t change anything. The question code is exactly the same as what you use? or is it a simplified version and the error may get lost in the way?– Sergio
@Sergio the code is this exactly, I copied and pasted.
– Allan Ramos
Puts
console.log(JSON.stringify(value));beforevar i = 0;. What returns?– Sergio
It appeared like this: {"outras_activities":"Other Activities","sem_activities":"S"} E in the second line. {"activity code":11}
– Allan Ramos