Laravel 500内部服务器错误但设置了CSRF令牌

时间:2018-01-15 10:11:43

标签: javascript php ajax laravel

我有一个新的Laravel项目,我自己开始。我想将3个字段保存到数据库中。我有一个帖子表,我有标题,帖子正文和作者字段。尝试保存时出现500 internal server错误。我检查了我的CSRF令牌 - 它们设置正确。我真的不确定问题出在哪里。

这是我的观点:

  <div class="row">
    <h1>Create a post</h1>
    <label for="title">Title</label>
    <input type="text" name="createTitle" id="createTitle">
    <label for="post">Post</label>
    <textarea name="createPost" rows="8" cols="80" id="createPost"></textarea>
    <label for="author">Author</label>
    <input type="text" name="createAuthor" id="createAuthor">
    <input type="submit" name="submit" value="Submit Post" id="submit">
  </div>

我的Ajax:

   $(document).ready(function(){
    $("#submit").on("click", function(e){
      e.preventDefault();
      var title = $("#createTitle").val();
      var body = $("#createPost").val();
      var author = $("#createAuthor").val();

      $.ajaxSetup({
        headers: {
          'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr("content")
        }
      });

      $.ajax({
        url:'/index/create',
        type: "POST",
        data: {
          title: title,
          body: body,
          author: author
        },
        success:function(response){
          $("#response").append("div" + response + "</div>");
        },
        error: function(error){
          $("#response").append("div" + error + "</div>");
        }
      });

    });
  });

我的路线:

    Route::post('/index/create', 'HomeController@create');

我的控制器:

public function create(Request $request){

  $post = new Post;
  $post->title = $request->input('createTitle');
  $post->post = $request->input('createPost');
  $post->author = $request->input('createAuthor');

  $post->save();

  $response = [
    'response' => 'successfully added',
    'error' => 'something went wrong'
  ];

  return response()->json($response);
}

0 个答案:

没有答案