Baca Hasil Json_encode PHP dengan Jquery

this is sampe code in php for encode data array to json with json_encode function

// kode di php
$data = array();
foreach($result as $key => $row) {
$record = array();
$record["nim"] = $row["nim"];
$record["nama_lengkap"] = $row["nama_lengkap"];
$data[] = $record;
}

echo json_encode($data);

 

to parse json ajax results in jquery is as follows

 $.ajax({
url: "/exam/url",
type: 'post',
crossDomain: true,
cache: false,
data: 'param=' + param,
}).then(function(response){
//data setelah downloaded 
//dan pass downloaded data
var json = $.parseJSON(response);
//now json variable contains data in json format
//let's display a few items
for (var i=0; i < json.length; ++i) {
$("#nim").val(json[i].nim);
$("#nama_lengkap").val(json[i].nama_lengkap);
}
});

 

hopefully useful, wassalam

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s