0

I'm parsing the json to get data but it is showing the undefined error.I'm using the following code.

JSON Data:-

 [{"id":"1","name":"vikash","email":"vikash@yahoo.com","phone":"98744254114"},false]

javascript:-

function getid(id) {
    //document.getElementById('pid').value=id;
    $.ajax({
        url: "page.php?id=" + id,
        success: function(result) {
            alert(result);
            var a = console.log(result.name);
            alert(a);

        }
    });
}​
gdoron
  • 147,333
  • 58
  • 291
  • 367
Ank Pro
  • 9
  • 1
  • 6

2 Answers2

5

Change:

var a = result.name;

To:

var a = result[0].name;

http://jsfiddle.net/GYr8Q/

Alex
  • 34,899
  • 5
  • 77
  • 90
  • that's because you're capturing the return value from `console.log(' ... ')`; don't - as there is nothing to capture. – Alex Jun 25 '12 at 23:18
  • Maybe `result` was not parsed yet. @Ank: Please have a look at http://stackoverflow.com/questions/7338442/using-a-json-response – Felix Kling Jun 25 '12 at 23:23
0

Result is an array, to get to the name use result[0].name

EDIT

As Felix King suggested the json might not be parsed, if you add a dataType:'json' to your ajax call it should be parsed.

function getid(id) {
    //document.getElementById('pid').value=id;
    $.ajax({
        url: "page.php?id=" + id,
        dataTpe: 'json',
        success: function(result) {
            alert(result);
            var a = console.log(result[0].name);
            alert(a);

        }
    });
}
Musa
  • 96,336
  • 17
  • 118
  • 137