Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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"}]
share|improve this question
    
side note: use console.log instead of alert thanks! –  zamnuts Nov 23 '13 at 10:46

3 Answers 3

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"

share|improve this answer
    
still undefined –  A.M Nov 23 '13 at 10:48
    
jsfiddle.net/kevincittadini/WGXh4 - It have to work (check your browser console to see results) –  Kevin Cittadini Nov 23 '13 at 10:58
    
@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 –  Kevin Cittadini Nov 23 '13 at 11:04
    
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) –  A.M Nov 23 '13 at 11:23

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']);
    }
});
share|improve this answer
    
no it gave an error on console –  A.M Nov 23 '13 at 10:48
    
tested your json object and it shows alert. Please check this jsFiddle: jsfiddle.net/BcvC3 –  Rajnikant Nov 23 '13 at 11:06
$.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']);
    }
});
share|improve this answer
    
same solution has been given before but it gives an error on console –  A.M Nov 23 '13 at 11:03
    
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); –  gnB Sep 16 '14 at 23:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.