Reputation: 477
In my php file I have
$data = array();
while ($row = mysql_fetch_array($result, true))
{$data[] = $row;};
echo json_encode($data);
It produces the JSON array:
[
{
"record_id": "4",
"eq_type_id": "999",
"scidiv_id_tag": "AKINS04",
"date_last_updated": "2011-07-11 14:41:58",
"description": "Optics Table D",
"eq_type_desc": "Other Equipment Type"
}
]
In my JQUERY script:
$.ajax(
{
type: "GET",
//dataType: "json",
url: "../scidiv/php/editData.php",
data: "recid=" + scidivtag,
success: function(data)
{
$('#output1').html(data);
$('#output').html("<b>id: </b>"+ data[0].record_id + "<b> name: </b>" +data[1].eq_type_id);
}
});
$('#output1').html(data); displays the above array on my web page....
But both
$('#output').html("Record Id: "+ data.record_id+"Eq Type ID: "+ data.eq_type_id);
an
$('#output').html("Record Id: "+ data[0].record_id+"Eq Type ID: "+ data[1].eq_type_id);
Gives me
id: undefined name: undefined
Can someone tell me what I'm missing??
Thanks
Chris
Upvotes: 0
Views: 153
Reputation: 477
Thanks for all the assistance and suggestions...
I solved the problem by re-writing my php script from scratch...
The main thing I did was get the results of the query as a simple array since I'm getting only one row of data the query:
$jqdata = mysql_fetch_row($result,);
echo json_encode($jqdata);
So my json looks like:
["7","1","999","AKINS01","2011-07-11 14:39:22","Optics Table A","Other Equipment Type"]
Thanks again!!
Upvotes: 0
Reputation: 10830
Get rid of the square braces wrapping it. You have an "array" with only one member (a map).
Without the square braces, data.eqType_id should work fine.
With the braces there, you always have to first look into member 0 of the array: data[0].eqType_id for example. But the extra level of nesting adds a layer that isn't necessary unless you are going to actually pass an array of maps.
Upvotes: 0
Reputation: 1391
Are you developing in Firefox or Chrome by any chance (IE sucks)? If so, use console.log to output the data coming back from the server side. It may be that jQuery isn't interpreting your response as JSON and therefore not parsing it. What is the response content type that is supplied? Make sure it is application/json. Either that, or you can force jQuery to parse it as JSON by setting the dataType to "json" for the AJAX request. See http://api.jquery.com/jQuery.ajax.
Upvotes: 1