1

How to access this json data in JavaScript. when I alert it the result is undefined

Here is jQuery code

$.ajax({
    type: "POST",
    url: "frmMktHelpGridd.php",
    data: {
        labNo: secondElement
    },
    dataType: "json",
    beforeSend: function () {
        // Do something before sending request to server
    },
    error: function (jqXHR, textStatus, errorThrown) {
        alert('error has occured');
        alert(errorThrown);
    },
    success: function (data) {

        //Here is the problem

        alert(data[0]['Result']);
    }
});

This is PHP code

            $data=array($no);
    for($i=0;($i<$no && ($row=mysql_fetch_array($result)));$i++)
    {
        $data[$i]=array();
        $data[$i]['Result']         =   $row['Result'];         
        $data[$i]['TestCode']       =   $row['TestCode'];           
        $data[$i]['TestStatus']     =   $row['TestStatus'];         
        $data[$i]['SrNo']           =   $row['SrNo'];               
    }

    $data1=json_encode($data);

    echo $data1;
      exit;

I have tested the PHP file independently, The json data is output as follows:

      [{"Result":"1","TestCode":"22","TestStatus":"0","SrNo":"1"},{"Result":"1","TestCode":"23","TestStatus":"1","SrNo":"2"}]
1
  • side note: use console.log instead of alert thanks! Commented Nov 23, 2013 at 10:46

3 Answers 3

2
$.ajax({
    type: "POST",
    url: "frmMktHelpGridd.php",
    data: {
        labNo: secondElement
    },
    dataType: "json",
    beforeSend: function () {
        // Do something before sending request to server
    },
    error: function (jqXHR, textStatus, errorThrown) {
        alert('error has occured');
        alert(errorThrown);
    },
    success: function (data) {
        //Added parse json
        var data = jQuery.parseJSON(data)

        alert(data[0]['Result']);
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

same solution has been given before but it gives an error on console
I tried a bunch of different answers on this site .. none worked; and this finally did the trick - thanks! In my case, I used the jQuery shortcut notation: var data = $.parseJSON(data);
0

You can access to your data by doing

data[0].Result

It's an Object, not an array.

so data[0]['Result'] it's not the proper way

EDIT: Since you have more objects, you have to do a loop this way:

$.each(data, function(key, val){
    console.log(val.Result);
    console.log(val.TestCode);  
    //...
});

When you see something like

{
    "foo":"bar",
    ...
}

you can access to it the same way as above:

name_of_the_object.foo

that will have the value "bar"

3 Comments

jsfiddle.net/kevincittadini/WGXh4 - It have to work (check your browser console to see results)
@sunny I apology I didn't give much attention to your JSON echo. I've seen only one object inside, but you got more so you have to do a loop, i'll update my answer too - This works :) jsfiddle.net/kevincittadini/WGXh4/2
Correct and thanx for your precious time. Solved My Problem. I can't vote your answer UP because my reputation is under 15 yet(sorry)
-1

Try to add parse JSON. I have added. Now it may be work.

$.ajax({
    type: "POST",
    url: "frmMktHelpGridd.php",
    data: {
        labNo: secondElement
    },
    dataType: "json",
    beforeSend: function () {
        // Do something before sending request to server
    },
    error: function (jqXHR, textStatus, errorThrown) {
        alert('error has occured');
        alert(errorThrown);
    },
    success: function (data) {
        //Added parse json
        var data = $.parseJSON(data)

        alert(data[0]['Result']);
    }
});

1 Comment

tested your json object and it shows alert. Please check this jsFiddle: jsfiddle.net/BcvC3

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.