500内部服务器错误Ajax Laravel [英] 500 Internal Server Error Ajax Laravel

查看:62
本文介绍了500内部服务器错误Ajax Laravel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试发布POST

I'm trying to make a POST to

http:// localhost:8888 / test

JS

$('.saveBTN').click(function (event) {

    $( "form#editForm" ).on( "submit", function( event ) {
      event.preventDefault();

      var inputs = {};
      $("#editForm :input").each(function() {
        inputs[$(this).attr("name")] = $(this).val();
      });

      var $inputs = JSON.stringify(inputs);

      $.ajax({
          headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
          url: '/test',
          type: 'POST',
          dataType: 'json',
          data: $inputs,
          success: function (data, textStatus, xhr) {

              console.log(data);
          },
          error: function (xhr, textStatus, errorThrown) {

              console.log('PUT error.');
          }
      });

});






我一直在


I keep getting


500内部服务器错误

500 Internal Server Error

我已尝试添加

< meta name =csrf-tokenvalue ={{csrf_token()}}>

这是我的Ajax

标题:{'X-CSRF-TOKEN': $('meta [name =csrf-token]')。attr('content')},

有关此的任何提示将非常感谢!

Any hints on this will be much appreciated !

推荐答案

在表格下方添加此内容。

Add this in below inside the form.

// This will generate token field which will be token
{{ csrf_field() }}  


// Expected Output
<input type="hidden" name="_token" value="ssdfdsfsdfsdfs32r23442">

VerifyCsrfToken.php - 文件是<包含在Web中间件组中的code> middleware 将自动验证请求输入中的标记是否与存储的标记匹配在会话中。

VerifyCsrfToken.php - The File is middleware, which is included in the web middleware group, will automatically verify that the token in the request input matches the token stored in the session.

请参阅此URL以获取更多信息:
http://laravel.com/docs/master/routing#csrf-x-csrf-token

Refer this URL for few more information: http://laravel.com/docs/master/routing#csrf-x-csrf-token


更新 - 2016年12月23日

Updates - 23rd Dec 2016

从laravel开始,您可以使用如下所示好吧。

As of laravel, you can use like below as well.

<form action="/foo/bar" method="POST">
    <input type="hidden" name="_method" value="POST">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>

API参考: https://laravel.com/docs/master/routing#form-method-spoofing

这篇关于500内部服务器错误Ajax Laravel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆