php - 500 内部服务器错误 Ajax Laravel

标签 php jquery ajax laravel laravel-5

我正在尝试向

发帖

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.');
          }
      });

});
<小时/>

我不断得到

500 Internal Server Error

我尝试添加

<meta name="csrf-token" value="{{ csrf_token() }}">

这在我的 Ajax 上

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

对此的任何提示将不胜感激!

最佳答案

将其添加到下面的表单中。

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


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

VerifyCsrfToken.php - 该文件是middleware,包含在Web中间件组中,会自动验证token中的token请求输入与 session 中存储的 token 匹配。

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

Updates - 23rd Dec 2016

从 Laravel 开始,您也可以像下面这样使用。

<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

关于php - 500 内部服务器错误 Ajax Laravel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34340304/

相关文章:

php - 我可以使用 PDO::FETCH_GROUP 按两个值对结果进行分组吗

php - CakePHP 表即使存在也丢失

php - 无法创建非静态方法 - fatal error

javascript - Kendo UI 和 Vue.js

javascript - 使用 video.js 的完整视频背景

php - 如何使用连接列导出到 csv

jquery - 显示 Firebase 存储中的所有图像

javascript - 如何将文本区域中的某些文本对齐为右对齐?

javascript - 图表未根据从 Jquery 收到的值进行更新

java - 尝试使用相同的 ajax 调用取消关注时,关注/取消关注按钮不起作用