-1

The ajax call returns a json object like this:

Object {0: "1", 1: "jake", 2: "#00ff00", tip_id: "1", tip_details: "jake", tip_color: "#00ff00"}

Object {0: "2", 1: "jakee", 2: "#00ff00", tip_id: "2", tip_details: "jakee", tip_color: "#00ff00"}

Object {0: "3", 1: "jakeee", 2: "#00ff00", tip_id: "3", tip_details: "jakeee", tip_color: "#00ff00"}

This is how i try to acces some values:

for(var i=0;i<=response.length-1;i++){
  console.log(response[i][1]);  //the result should be: jake,jakee,jakee
}

I also tried:

for(var i=0;i<=response.length-1;i++){
      console.log(response[i].tip_details);  //the result should be: jake,jakee,jakee
}

I just can't get them,and i dont know why,am i missing something?

Petru Lebada
  • 2,167
  • 8
  • 38
  • 59
  • 1
    try `console.log(response)`, what is the output? Maybe it has a `data` property you need to use, I seem to remember that from something a long time back – musefan Jun 09 '15 at 13:47
  • 2
    How are you receiving three separate objects, exactly? Let's see the code that listens for the AJAX response, if we could. – Shotgun Ninja Jun 09 '15 at 13:48
  • 1
    Log the `response` object like @musefan said then copy and past the output here... – brso05 Jun 09 '15 at 13:48
  • If @musefan is right, then you might have to get each of the objects from `response` via `response.data[i]`, which will give you the `Object`s you listed above. – Shotgun Ninja Jun 09 '15 at 13:49
  • Use `console.log(JSON.stringify(response));` and then copy and paste here that will help us help u... – brso05 Jun 09 '15 at 13:49
  • @ShotgunNinja , that's because i'm requesting a query from a database,and i loop through it in my js code. – Petru Lebada Jun 09 '15 at 13:53
  • 1. do `console.log(response)` 2. copy the output and paste it in your question 3. problem will be solved... it's as easy as abc – musefan Jun 09 '15 at 13:54
  • @musefan , the objects above,are outputed by console.log . There's nothing else. – Petru Lebada Jun 09 '15 at 13:57
  • Then I think we need to see the code that is making the ajax call.. Also, what server side technology are you using? – musefan Jun 09 '15 at 13:58
  • @musefan php is what i use,anyway i found the error...it was just a little syntax error...thanks everyone anyway – Petru Lebada Jun 09 '15 at 14:00

2 Answers2

1

That is not a JSON object. JSON notation only allows keys to be string and not numbers.

Consider taking a look at an answer to a similar question here.

Community
  • 1
  • 1
pgrodrigues
  • 2,083
  • 1
  • 24
  • 28
0

Those are not JSON. Those are JS objects. JSON keys are ONLY String.

check what is in your response using console.log(response). Considering that response is javascript array of objects, following code should return the expected output.

for(var i=0; i<=response.length-1; i++){
  console.log(response[i][1]);
}
shankulk
  • 534
  • 1
  • 4
  • 23