bucle de los pares clave/valor

I'm trying to loop the key/value pairs I receive below in my query and console out the email address. What I'm I doing wrong?

JSON I receive...

{"ERRORS":[],"DATA":[{"INCENTIVEID":"1","CREATED":"","EMAIL":"email","RECIPIENTID":"1","NAME":"glyn","ACTIVE":0,"MODIFIED":"","MOBILE":"11111111111"},{"INCENTIVEID":"1","CREATED":"","EMAIL":"eee","RECIPIENTID":"2","NAME":"edem","ACTIVE":0,"MODIFIED":"","MOBILE":"11111111111"}],"MESSAGES":[]}

Current Script

$(document).ready(function(){
        var digits = /^\d{11}$/;
        $("#mobile").on("keyup keypress", function(){

            if (digits.test(this.value)) {
                $.ajax({
                    url: "http://api.domain.com/recipients/lookup",
                    data: {
                        mobile: this.value,
                        incentiveID:  $("#incentiveID").val()
                    },
                    success: function(data){
                        $.each(data.DATA, function(index, value) {
                          console.log(value.EMAIL);
                        });
                         console.log(data);
                    }
                });
            }
        });
});

preguntado el 28 de agosto de 12 a las 12:08

this part " console.log(value.EMAIL);" give me undefined. How do I get all the email addresses returned from the Json call? -

2 Respuestas

The e-mails are within the DATA array; you should use data.DATA en lugar de solo data, which refers to the entire JSON object:

$.each(data.DATA, function(index, value) {
  console.log(value.EMAIL);
});

Respondido 28 ago 12, 12:08

I tried that but with the above code this gave me an error: TypeError: a is undefined - Prometeo

@Spike: You sure you don't have a typo? I don't reference any variable called a in the above code. Can you paste your new code? - Juan Carlos

edit code above. error is TypeError: a is undefined [Break On This Error] ...=[]),o&&(e=" "+e);while(e){k=!1;if(j=B.exec(e))e=e.slice(j[0].length),k=d.push({... - Prometeo

That's a jQuery error. Can you paste me the exact content of data? Is it a string or a JSON object? - Juan Carlos

That is because there is a null value provided to the $.each function. It is a jquery error, it more than likely comes from your JSON output than the jquery loop. - David Barker

Added below and issue was resolved. anyone know why?

type: "GET", dataType: "json",

Respondido 28 ago 12, 14:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.