2016-10-29 114 views
1

我正在使用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 
]); 
} 
+0

首先,你需要你的路線改變後的路線。在您的ajax對象中,當您發佈數據時,您將類型設置爲POST,您必須對您的路線執行相同的操作。 其次,你有沒有試過在你的calendarStore方法中檢查你的網絡選項卡和dd($ request)?如果不嘗試這個,看看你是否得到你的控制器方法。 第三,在你的ajax對象中添加一個錯誤鍵,它在'success:function(){...}'下面以同樣的方式接受一個閉包,'error:function(response){console.log('here',響應); }'以查看發佈時是否發生錯誤。 –

+0

有什麼錯誤?必須記錄AJAX錯誤或apache日誌錯誤。 –

回答

1

取代你的AJAX的聲明data參數是這樣的:

//creates a new event on the calendar 
eventData = { 
    "title": title, 
    "start": start, 
    "end": end, 
    "allDay": allDay 
}; 
+0

報價屬性僅適用於無效的標識符。我不認爲這是問題,肯定還有別的。 –

0

我做了以下修改:如預期,一旦用戶點擊/選擇在特定的一天/天的IT將其工作請求標題並將事件保存在數據庫中。

select: function(start, end, allDay) { 
    var title = prompt('Are u sure you want to apply for job? Yes/No:');// Event Title: 
    var start_time = moment(start).format(); 
    var end_time = moment(end).format(); //onclick get date 
    var eventData; 
    if (title) { 
     eventData = { 
      title: title, 
      start: start, 
      end: end, 
      allDay: allDay 
     }; 
     $('#calendar').fullCalendar('renderEvent', eventData, true); // stick? = true 

     $.ajax({ 
      type: 'GET',//POST changed to GET 
      url: "/projects/calendar/store", // your url             
      data: { //event data from global variable 
       title: event_name, 
       start: start_time, 
       end: end_time, 
       event_id:event_id 
      }, 
      beforeSend: function (request) { 
       return request.setRequestHeader('X-CSRF-Token', $("meta[name='csrf-token']").attr('content')); 
      }, 
      success: function(response) { 
       console.log(response); 
      } 
     }); 
    } 
    $('#calendar').fullCalendar('unselect'); 
}, 

更新的路由

Route::get('/projects/calendar/store','[email protected]'); 

更新控制器

public function calendarStore(Request $request) 
{ 
    $userId = Auth::user()->id; 
    $event_title =$request->get('title'); 
    $start =$request->input('start'); 
    $end =$request->input('end'); 
    $project_id= $request->input('event_id');  


    $calendar_event = Calendar::create([ 
     'user_id' => $userId, 
     'project_id' => $project_id, 
     'event_title' => $event_title, 
     'start_date' =>$start, 
     'end_date' => $end 
    ]);  
    return $calendar->all(); 
} 
相關問題