I want to process a Json
array returned by php
file. The PHP
code is below.
$pdo = new PDO('');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = 'SELECT * from table';
foreach ($pdo->query($sql) as $row)
{
$results[] = array('row1' =>$row['col1'],
'row2' => $row['col2'],
'row3' => $row['col3']);
}
header('Content-Type: application/json');
echo json_encode($results);
The above code will return Json
array like this one
[
{"row1":"test","row2":"5.123456","row3":"6.123456"},
{"row1":"test1","row2":"6.123456","row3":"8.123654"},
{"row1":"test3","row2":"6.321456","row3":"8.964512"}
]
I want to process the above Json
array and present it inside a table. I try many Jquery
codes and spent hours in front of computer. But i can't get correct answer. Please help me. Help me by either give correct Jquery
code to process the Json
array or give correct PHP
code to send correct Json
string. I forgot to say another info. When I try to debug using FF, I am getting an error in console
http://i.stack.imgur.com/tnLcZ.jpg
(image shows: SyntaxError: JSON.parse: unexpected end of data)
UPDATE
I update your script like this. The script return nothing. Please help
var html="<table border=1 style='border-collapse:collapse'>";
$(document).ready(function () {
$.ajax({
url: "http://10.44.0.160/service.php",
contentType: "application/json; charset=utf-8",
dataType: "jsonp",
type: "POST",
success: function(data)
{
data.forEach(function(obj,index){html+="<tr><td>"+index+"<td>"+obj.csc_name+"</td><td>"+obj.lat+"</td><td>"+obj.longi+"</td></tr>";});
html+="</table>";
jQuery(html).appendTo("body");
}
});
});
I try many Jquery codes and spent hours in front of computer.
Could you please post some of your attempts? – palaѕн Oct 4 '13 at 10:35