我正在使用fullcalendar,當用戶點擊某個特定的日期時,我需要創建一個新事件,我使用Laravel控制器來存儲由ajax請求發送的數據。我可以在日曆上創建一個事件,但我無法將數據發送給控制器,而且創建事件後,瀏覽器選項卡也會凍結。任何人都可以糾正我錯在哪裏?或爲問題提供更好的解決方案。發送Ajax數據到Laravel控制器
select: function(start, end, allDay) {
var title = event_name;// Event Title:
var eventData;
if (title) {
eventData = { //creates a new event on the calendar
title: title,
start: start,
end: end,
allDay: allDay
};
$('#calendar').fullCalendar('renderEvent', eventData, true); // stick? = true
$.ajax({
data:eventData,
type: 'POST',
url:"/projects/calendar/store", // your url
beforeSend: function (request) {
return request.setRequestHeader('X-CSRF-Token', $("meta[name='csrf-token']").attr('content'));
},
success: function(response) {
console.log(response);
}
});
}
$('#calendar').fullCalendar('unselect');
},
這是我的路由文件,我已經嘗試了get和post,但都沒有工作。
Route::get('/projects/calendar/store','[email protected]');
這是控制器,它只處理由ajax請求發送的數據,但不處理數據。
public function calendarStore(Request $request)
{
$calendar = new Calendar;
Input::all();
$userId = Auth::user()->id;
$event_title =$request->input('title');
$start =$request->input('start');
$end =$request->input('end');
$calendar_event = Calendar::create([
'user_id' => $userId, // I can get the user ID
'project_id' => 2,
'event_title' => $event_title,
'start_date' =>$start,
'end_date' => $end
]);
}
首先,你需要你的路線改變後的路線。在您的ajax對象中,當您發佈數據時,您將類型設置爲POST,您必須對您的路線執行相同的操作。 其次,你有沒有試過在你的calendarStore方法中檢查你的網絡選項卡和dd($ request)?如果不嘗試這個,看看你是否得到你的控制器方法。 第三,在你的ajax對象中添加一個錯誤鍵,它在'success:function(){...}'下面以同樣的方式接受一個閉包,'error:function(response){console.log('here',響應); }'以查看發佈時是否發生錯誤。 –
有什麼錯誤?必須記錄AJAX錯誤或apache日誌錯誤。 –