173
rated 0 times
[
176]
[
3]
/ answers: 1 / hits: 17707
/ 13 Years ago, wed, march 23, 2011, 12:00:00
I'm making an ajax call using jQuery as below.
$.ajax({
type: POST,
url: proc.php,
dataType: 'json',
data: dataString,
cache: false,
success: function(data){
alert(data.vote[0].line); //Where error shows
}
});
The php page returns echo json_encode($string);
which is like
{ 'vote' : [{ 'line' : 'newline1', 'up' : '0', 'down' : '1'},
{ 'line' : 'newline2', 'up' : '4', 'down' : '1'}
]}
When I run it, an error comes up saying
Uncaught TypeError: Cannot read property '0' of undefined
on the line commented above in the ajax call
Can anyone help me point out where am I doing it wrong??
UPDATE:
the variable $string
is generated as below
$comma = ,;
$success = mysql_query($query, $connection);
while($row = mysql_fetch_array($success)){
$voteUp = $row['voteup'];
$voteDwn = $row['votedwn'];
$vote .= $comma . { 'line' : '{$row['entryid']}', 'up' : '{$voteUp}', 'down' : '{$voteDwn}';
$comma = ,;
}
$string = { 'vote' : [ . $vote . ]};
echo json_encode($string);
More From » jquery