I am returning a JSON array to my jQuery function script, but cannot seem to get the syntax correct -- even after reading about a dozen different examples here.
Here is the calling jQuery ajax function:
$.ajax({
url: "fetchAlleles.php",
datatype: 'json',
data: {'mndx': mndx, 'sndx': sndx },
success: function(rtnval) {
alert("success: allele_1="+rtnval['allele_1']+", allele_2="+rtnval['allele_2']);
//alert(rtnval);
},
error: function() { alert('Error!'); }
});
Here is the PHP code that returns the values:
echo "{";
echo "allele_1: ", json_encode($ary[0]), "\n";
echo "allele_2: ", json_encode($ary[1]), "\n";
echo "run_date: ", json_encode($ary[2]), "\n";
echo "}";
If I use my alert debugging statement, I can see what's being returned is what I expect:
{allele_1: "440"
allele_2: "480"
run_date: null
}
but anything else I try to read the values inside the JSON object comes back "undefined", whether it's
alert("success: allele_1="+rtnval['allele_1']+", allele_2="+rtnval['allele_2']);
or
alert("success: allele_1="+rtnval.allele_1+", allele_2="+rtnval.allele_2);
or
alert("success: allele_1="+rtnval['allele_1'][0]+", allele_2="+rtnval['allele_2'][0]);
Hope someone can help -- this is exasperating! (Wish JSON syntax were more obvious!)
thanks much, rixter