0

I get JSON response from a server:

[{"id":605,"vote":1},{"id":606,"vote":-1},{"id":611,"vote":1},{"id":609,"vote":-1}]

Then I try to loop through results and get objects properties:

success: 
function (data) {
$.each(data, function() {
$.each(this, function(i, v) {
alert(i+v);
});    
});
}

But somehow my code fails and no alert is shown. What am I doing wrong guys?

Saulius s
  • 601
  • 2
  • 9
  • 18

3 Answers3

1

Specify dataType to "json" in your ajax request:

 $.ajax({
     //...
     dataType:"json",
     success://etc...
Engineer
  • 47,849
  • 12
  • 88
  • 91
0

Assuming there are no parsing issues with the string being in correct JSON format, you could do:

function (data) {
$.each(data, function() {
 console.log(this.id);
console.log(this.vote);
});
}
Code Monkey
  • 643
  • 6
  • 18
0

Try this:

var data = [{"id":605,"vote":1},{"id":606,"vote":-1},{"id":611,"vote":1},{"id":609,"vote":-1}];
$.each(data, function( index, value ) {
  console.log( index + ": " + value.id + ", " + value.vote );
});
John McNulty
  • 293
  • 4
  • 11