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":"[email protected]","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);

        }
    });
}​
2
  • you are parsing an array with the json as first element! Commented Jun 25, 2012 at 23:09
  • Well, result is an array if it was parsed correctly. If not, it is a string which you have to parse first. Commented Jun 25, 2012 at 23:21

2 Answers 2

5

Change:

var a = result.name;

To:

var a = result[0].name;

http://jsfiddle.net/GYr8Q/

Sign up to request clarification or add additional context in comments.

2 Comments

that's because you're capturing the return value from console.log(' ... '); don't - as there is nothing to capture.
Maybe result was not parsed yet. @Ank: Please have a look at stackoverflow.com/questions/7338442/using-a-json-response
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);

        }
    });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.