也许这是一个非常常见的主题,但是我找不到任何解决方案!,当我尝试console.log时,我尝试将ajax发布到控制器,数据在那里,但数据没有发送到我的控制器,这是我的console.log的屏幕截图,我需要将jquery ajax的一些数据发送到laravel控制器,以便我拥有带有csrf令牌的元数据,当我使用ajax将发布请求发送到URL时,它只是发送了令牌!但我没有发送的任何数据。
这是我的javaScript代码:-
function doComment(lesson_id, parent_id) {
var body = $('#textbody'+parent_id).val();
var file_data = $('#image').prop("files")[0];
if (body == '') {
alert('Harap Isi Komentar !')
}else {
var postData =
{
"_token":$('meta[name="csrf-token"]').attr('content'),
"lesson_id": lesson_id,
"parent_id": parent_id,
"image" : image,
"body": body
}
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
type :'POST',
url :'{{ url("lessons/coments/doComment") }}',
dataType: 'json',
cache: false,
contentType: false,
processData: false,
data : postData,
beforeSend: function(){
console.log(postData);
// Show image container
swal({
title: "Sedang mengirim Komentar",
text: "Mohon Tunggu sebentar",
imageUrl: "{{ asset('template/web/img/loading.gif') }}",
showConfirmButton: false,
allowOutsideClick: false
});
{{-- $("#loader").show(); --}}
},
success:function(data){
if (data.success == false) {
window.location.href = '{{ url("member/signin") }}';
}else if (data.success == true) {
$('#textbody'+parent_id).val('');
swal({
title: "Komentar anda sudah terkirim!",
showConfirmButton: true,
timer: 3000
});
getComments();
}
}
});
}
}
这是我的控制器
public function doComment()
{
$response = array();
if (empty(Auth::guard('members')->user()->id)) {
$response['success'] = false;
} else {
$now = new DateTime();
$uid = Auth::guard('members')->user()->id;
$body = Input::get('body');
$lesson_id = Input::get('lesson_id');
$member = DB::table('members')->where('id', $uid)->first();
$lessons = DB::table('lessons')->where('id', $lesson_id)->first();
$parent_id = Input::get('parent_id');
$contri = Lesson::where('id',$lesson_id)
->select('contributor_id')
->first();
// dd($lesson_id);
$image = Input::file('image');
// dd($image);
$lessonsDestinationPath= 'assets/source/komentar';
if(!empty($image)){
$imagename = $image->getClientOriginalExtension();
$image->move($lessonsDestinationPath, $imagename);
}else{
$imagename = '';
}
if($imagename ==''){
$url_image= $imagename;
}else{
$urls=url('');
$url_image= $urls.'/assets/source/komentar/'.$imagename;
}
$store = DB::table('comments')->insertGetId([
'lesson_id' => $lesson_id,
'member_id' => $uid,
'body' => $body,
'parent_id' => $parent_id,
'status' => 0,
'desc' => 0,
'images' => $url_image,
'contributor_id' => str_replace('}','',str_replace('{"contributor_id":', '',$contri)),
'created_at' => $now,
'updated_at' => $now,
]);
$getmembercomment = DB::table('comments')
->Join('members','members.id','=','comments.member_id')
->where('comments.lesson_id',$lesson_id)
->where('comments.parent_id',0)
->where('comments.status',1)
->select('comments.*','members.username as username')
->first();
$getemailchild = DB::table('comments')
->Join('comments as B', 'comments.id', 'B.parent_id')
->Where('B.parent_id', $parent_id)
->where('comments.member_id', '<>', 'B.member_id')
->select('comments.member_id as tanya', 'B.member_id as jawab')->distinct()
->get();
// dd($getemailchild);
if($parent_id != 0){
foreach ($getemailchild as $mails) {
// Check type
if (is_array($mails)){
// Scan through inner loop
foreach ($mails as $value) {
$member = Member::Find($value);
$lesson = Lesson::Find($lesson_id);
$contrib = Contributor::find($lessons->contributor_id);
$member->notify(new UserReplyNotification($member, $lesson, $contrib));
}
}
}
}
if ($store) {
// dd($store);
DB::table('contributor_notif')->insert([
'contributor_id' => $lessons->contributor_id,
'category' => 'Komentar',
'title' => 'Anda mendapat pertanyaan dari ' . $member->username,
'notif' => 'Anda mendapatkan pertanyaan dari ' . $member->username . ' pada ' . $lessons->title,
'status' => 0,
'created_at' => $now,
]);
$member = Member::Find($uid);
$comment = Comment::Find($store);
$lesson = Lesson::find($lessons->id);
$contrib = Contributor::find($lessons->contributor_id);
$contrib->notify(new UserCommentNotification($member, $comment, $contrib, $lesson));
$response['success'] = true;
}
}
echo json_encode($response);
}
这是我的路线:
Route::post('lessons/coments/doComment','Web\LessonsController@doComment');
答案 0 :(得分:0)
不确定laravel如何处理发布数据(是否可以处理,但正常的$ _POST和php:// input)。您可以尝试从Javascript Ajax代码中删除datatype :JSON
。也删除cache: false,
contentType: false,
processData: false,
;这些参数是可选的,除非有特殊原因需要使用json数据类型,否则应使用默认值。同样,您可以尝试先使用$received = file_get_contents("php://input");
然后使用print_r($received)
在控制器上接收数据。看它是否捕获了JSON数据