我有一个像这样的脚本:
$(document).on("click", ".passingID", function () {
var ids = $(this).attr('data-id');
$("#idkl").val( ids );
$.ajax({
method: "POST",
url: "getidforeditkaryawan.php",
data: { idkl: ids}
}).done(function(data) {
console.log(data);
$("#nik").val(data['nik']);
$("#ktp").val(data['ktp']);
$("#status").val(data['status']);
$("#nama").val(data['nama']);
$("#bank").val(data['bank']);
$("#norek").val(data['norek']);
$("#tglmsk").val(data['tgl_msk']);
$("#tglkl").val(data['tgl_keluar']);
$("#idp").val(data['id_perusahaan']);
$("#tlk").val(data['tlk_individu']);
}).fail(function( jqXHR, textStatus ) {
console.log(jqXHR, textStatus);
});
});
这是getidforeditkaryawan.php文件:
<?php
$con = mysqli_connect("127.0.0.1","root","","penggajian");
$sql = "SELECT * FROM karyawan_lapangan WHERE id_kl = '".$_POST['idkl']."'";
$getdatakaryawan = mysqli_query($con,$sql);
$data = mysqli_fetch_assoc($getdatakaryawan);
print_r(json_encode($data));
?>
但是每次我尝试像这样在'data'变量中打印值
console.log(data['nik']);
该值不会显示
print_r(json_encode($data));
的结果是:
{"id_kl":"4","nik":"13","ktp":"1231231","status":"aktif","nama":"ardia","bank":"asdasd","norek":"21313","tgl_masuk":"2019-02-14","tgl_keluar":"0000-00-00","id_perusahaan":"3123121","tlk_individu":"4124124"}
答案 0 :(得分:2)
由于您返回了json对象,因此您可以像这样访问它:
$("#nik").val(data.nik);
根据04FS注释进行编辑:
$.ajax({
method: "POST",
dataType: "json",
url: "getidforeditkaryawan.php",
data: { idkl: ids}
}).
错过那个地方,他是对的。