使用Ajax发布的Laravel中出现500错误

时间:2017-03-22 20:33:35

标签: php jquery ajax laravel-5

我有6种不同的路线可以从输入选择中选择。然后,每个选定的路线将发布到自己的数据库

问题是我得到了所有这些的500错误,但是其中一半,它实际上发布到数据库。我已经逐行完成了,除了变量名之外,代码是相同的。这是一个根本不起作用的例子。

submit.js

  $('#submit-event').on('click', function() {
    event.preventDefault()
    let title = $('#title').val()
    let type = $('#type').val() // for selecting which DB
    let start = $('#start').data('DateTimePicker').date()
    let end = $('#end').data('DateTimePicker').date()

    let data = {
      '_token': token,
      'title': title,
      'start': start,
      'end': end
    }

    console.log(type); // logs the correct POST route

    $.ajax({
      method: 'POST',
      url: type,
      data: data,
      success: function(data) {
        console.log(data);
      },
      error: function(err) {
        console.log(err)
      }
    });

  })

routes.php文件

Route::post('/createmeeting', [
  'uses' => 'MeetingController@postCreateMeeting',
  'as' => 'createmeeting'
]);

MeetingController.php

class MeetingController extends Controller
{

    // Get Meeting from DB - works
    public function getMeetings()
    {
      $meetings = Meeting::orderBy('created_at', 'desc')->get();
      return $meetings;
    }

    // Add new Meeting to DB - doesn't work (500 error)
    public function postCreateMeeting(Request $request)
    {
      if (!request['_token']) {
        return redirect()->route('calendar')->with(['message' => "You must be logged in"]);
      }
      // Save Meeting
      $meeting = new Meeting();
      $meeting->title = $request['title'];
      $meeting->start = $request['start'];
      $meeting->end = $request['end'];

      if ($request->user()->meetings()->save($meeting)) {
        $message = 'Event successfully added to calendar';
        return redirect()->route('calendar')->with(['message' => $message]);
      }
      return redirect()->route('calendar')->with(['message' => $message]);
    }
}

对类似问题的回应表明令牌存在问题,但我在此测试。知道错误发生的地方吗?

0 个答案:

没有答案