Postingan lainnya
hasil panggil data undefined
permisi gan, saya mau tanya. jadi saya lgi buat sistem komentar untuk tiap postingan dengan AJAX, data berhasil masuk, tapi pas di console log nilainya undefined, begitupula saat komentar baru muncul isinya berubah jadi 'undefined'. tp klo direfresh baru muncul isi komentar aslinya yg baru. gimana caranya panggil data di prepend() ya? soalnya kalo di tutorial tinggal data.namakolom gitu. mohon bantuannya terima kasih
@auth
<script>
$(document).ready(function() {
// on button click we are getting values by input name.
$(".komentar").click(function(e){
e.preventDefault();
var _token = $("input[name='_token']").val(); // get csrf field.
var isi = $("#isi").val();
var userId = '{{Auth::user()->id}}';
var username = '{{Auth::user()->name}}';
var avatar = '{{Auth::user()->gravatar}}';
var undanganId = '{{$undangan->id}}';
if(isi == '') return false;
$('#isi').val("");
$.ajax({
url: "{{route('komentar')}}",
type:'POST',
dataType: "json",
data: { _token:_token, isi:isi, userId:userId, undanganId:undanganId},
success: function(data) {
console.log(data.isi);
$('.add-new').prepend('<div class="media-left"><img class="media-object" style="width:55px;border-radius:100px;float:left" src="' + avatar + '" /></div><div class="media-body "> <h5 class=media-heading><b>'+username+' </b></h5><blockquote style="font-size:13px">' + data.isi + '</blockquote> <div class="btn-group" role="group" aria-label="..."><button type="button" class="tombol-hapus btn btn-xs btn-default"><span class="glyphicon glyphicon-trash"></span> Edit</button><button type="button" class=" tombol btn btn-xs btn-default" data-tombol="2"><span class="glyphicon glyphicon-edit"></span> Ubah</button></div></div><div class="media-right" ><div style="width:100px"><i style="font-size:10px"><span class="glyphicon glyphicon-time"></span> Baru Saja</i></div></div><hr>').fadeIn('slow')}});
})
});
</script>
@endauth
0
1 Jawaban:
coba komentarnya dibuat kayak gini
$(document).ready(function() {
// on button click we are getting values by input name.
$(document).on('click', '.komentar' , function(e){
e.preventDefault();
var _token = $("input[name='_token']").val(); // get csrf field.
var isi = $("#isi").val();
var userId = '{{Auth::user()->id}}';
var username = '{{Auth::user()->name}}';
var avatar = '{{Auth::user()->gravatar}}';
var undanganId = '{{$undangan->id}}';
if(isi == '') return false;
$('#isi').val("");
$.ajax({
url: "{{route('komentar')}}",
type:'POST',
dataType: "json",
data: { _token:_token, isi:isi, userId:userId, undanganId:undanganId},
success: function(data) {
console.log(data.isi);
$('.add-new').prepend('<div class="media-left"><img class="media-object" style="width:55px;border-radius:100px;float:left" src="' + avatar + '" /></div><div class="media-body "> <h5 class=media-heading><b>'+username+' </b></h5><blockquote style="font-size:13px">' + data.isi + '</blockquote> <div class="btn-group" role="group" aria-label="..."><button type="button" class="tombol-hapus btn btn-xs btn-default"><span class="glyphicon glyphicon-trash"></span> Edit</button><button type="button" class=" tombol btn btn-xs btn-default" data-tombol="2"><span class="glyphicon glyphicon-edit"></span> Ubah</button></div></div><div class="media-right" ><div style="width:100px"><i style="font-size:10px"><span class="glyphicon glyphicon-time"></span> Baru Saja</i></div></div><hr>').fadeIn('slow')}});
});
});
semoga berhasil
0