Laravel 5:Ajax Post 500(内部服务器错误)

我正在尝试通过ajax将数据提交到数据库。 提交文章页面没有ajax就可以正常工作。 我添加了console.log()只是为了看看是否有任何事情发生,但我得到了这个错误:

POST http:// localhost / laravel-5 / public / articles / create 500(内部服务器错误)

我的代码出了什么问题? 是javascript还是控制器?

编辑:我在laravel.log得到这个

C:\ xampp \ htdocs \ laravel-5 \ vendor \ laravel \ framework \ src \ Illuminate \ Foundation \ Http \ Middleware \ VerifyCsrfToken.php中的exception’Illuminate \ Session \ TokenMismatchException’:53

路线

 Route::resource('articles', 'ArticlesController'); 

调节器

 public function store(Requests\ArticleRequest $request) { $article = new Article($request->all()); Auth::user()->articles()->save($article); $response = array( 'status' => 'success', 'msg' => 'Article has been posted.', ); return \Response::json($response); } 

jQuery的

 $(document).ready(function() { $('#frm').on('submit', function (e) { e.preventDefault(); var title = $('#title').val(); var body = $('#body').val(); var published_at = $('#published_at').val(); $.ajax({ type: "POST", url: 'http://localhost/laravel-5/public/articles/create', dataType: 'JSON', data: {title: title, body: body, published_at: published_at}, success: function( data ) { $("#ajaxResponse").append(data.msg); console.log(data); } }); }); 

视图

  

Write a New Article


{!! Form::open(['url' => 'articles', 'id' => 'frm']) !!}

{!! Form::label('title', 'Title:') !!} {!! Form::text('title') !!}

{!! Form::label('body', 'Body:') !!} {!! Form::textarea('body') !!}

{!! Form::label('published_at', 'Date:') !!} {!! Form::input('date', 'published_at', date('Ym-d'), ['class' => 'form-control']) !!}

{!! Form::submit('Submit Article', ['id' => 'submit']) !!}

{!! Form::close() !!}

@if($errors->any())
    @foreach($errors->all() as $error)
  • {{ $error }}
  • @endforeach
@endif

});

当您通过POST向资源控制器发出请求时,它会自动调用store方法:

 Verb Path Action Route Name ---------------------------------- POST /articles store articles.store 

所以,你只需要将ajax url更改为:

 $.ajax({ type: "POST", url: 'http://localhost/laravel-5/public/articles', 

当您需要发送会话令牌时,您可以添加一个全局元标记,例如这是您的网站:

  

然后,只需通过ajax的标头添加令牌:

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

如果您使用的是Form::open()函数(LaravelCollective),则会添加一个隐藏的输入,并将该标记作为值,名称为_token 。 因此,您可以删除元标记并编辑您的ajax标题,如下所示:

 $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('[name="_token"]').val() } }); 

这就是我在C:\ xampp \ htdocs \ laravel-5 \ vendor \ laravel \ framework \ src \ Illuminate \ Foundation \ Htt p \ Middleware \ VerifyCsrfToken.php中得到exception’Illuminate \ Session \ TokenMismatchException’:53

你正在使用Laravel的CSRF保护。

http://laravel.com/docs/5.1/routing#csrf-protection

您需要传递隐藏的_token字段的值。 这可以通过在应用程序的JS中执行此操作,在所有jQuery启动的AJAX请求上自动完成:

 $.ajaxSetup({ headers: { 'X-CSRF-TOKEN': $('input[name="_token"]').value() } }); 

或者,您可以在每个AJAX调用中手动获取并传递_token隐藏字段的值。

我喜欢分享这段代码,以帮助某人需要ajaxpost并使用laravel

 <<<<<<<< POST <<<< < @section('content') 
-------------0---------------------- -------------0---------------------- <<<----<name('login4'); ------------------0---------------------- ------------------0---------------------- <<<< Get < @section('content')
-------------0---------------------- -------------0---------------------- <<<----<name('login2'); -------------0---------------------- -------------0---------------------- <<<----<isMethod('get')) { return response()->json(['response' => 'This is get method']); } return response()->json(['response' => 'This is post method']); } -------------0---------------------- -------------0----------------------

您可以将您的URL添加到VerifyCsrfToken.php中间件。 这些url将从CSRFvalidation中排除:

 protected $except = [ "your url", "your url/abc" ]; 

好吧,如果你正在寻找确定的镜头答案,这里是:如果您在代码中缺少csrf_token(),则会出现此错误以下是我所做的,

 
ITEMS ORDERED:CLICK HERE

现在使用Ajax

  

在我的laravel控制器中

 public function getcart(Request $req) { return response ("its"); }