YVS1102 YVS1102 - 6 months ago 10
jQuery Question

Receive array from ajax request

Good day, Please check my script first.



$(document).ready(function() {

$("#findmynip").click(function() {
$("#hasilnip").html('<img src="<?=base_url();?>assets_global/images/loader.gif">&nbsp; Please wait');
var nipnnya = $("#nipnya").val();
$.ajax({
type: "POST",
data: {
nip: nipnnya
},
datatpe: 'json',
url: "<?=base_url();?>registrasi/cek_ada/",
success: function(hslnip) {
if (hslnip) {
alert(hslnip);
$("#name").html(hslnip);
//How do i do this
$("#name").html(hslnip['FullName']);
$("#birthday").html(hslnip['BirthDate']);
} else {
$("#name").html('Failed');
}

}
});
return false;
});
});





What i want to is receive the ajax request to my html.

When i try to
console.log(hslnip);
the result is
{"FullName":"BUNGA","BirthDate":"1994-10-03 00:00:00.000"}
. Any help would be appreciated.

What i want to receive is

<div id='name'></div>
<div id='birthday'></div>


I made typo at this part :
datatpe: 'json',
and i change it to
dataType: 'json'
. It's working now, thans for helping guys.

Answer

You cannot remove an array in your situation. You can use It do build an HTML content which can be displayed in hasilnama element.

Change this:

$("#hasilnama").html(hslnip);

To this for example:

$("#hasilnama").html('<span>Fullname: ' + hslnip.FullName + '</span><br><span>Birthdate: ' + hslnip.BirthDate.substring(0, 10) + '</span>');

Comments