【问题标题】:POST 500 internal server errorPOST 500 内部服务器错误
【发布时间】:2018-01-06 01:06:37
【问题描述】:

我正在尝试学习如何将值从 jquery 传递到控制器并将其插入数据库,而不是在 laravel 5.4 中将值从表单传递到控制器,但不幸的是我收到了这个错误消息:

POST http://x.x.x.x:x/addtable 500(内部服务器错误)。

这是我的刀片文件:SampleInsert.blade.php

{{csrf_field()}}
<input type="text" id="sample2"  required>
<div>
  <button type="button" class="button" onclick = "submit();"> SUBMIT</button>
  <a href="{{ url ('/') }}"><button type="button" class="button"> Cancel</button></a>
</div>

jquery:

function submit() {
  var samp = Array();
    samp[ 0 ] = 1;
    samp[ 1 ] = 'a';
    samp[ 2 ] =  document.getElementById( "sample2" )
    .value;
    samp[ 3 ] = 2;
    samp[ 4 ] = 3;

  var sample = JSON.stringify( samp );
  $.ajax( {
      type: "POST",
      url: '/addtable',
      data: {
          pSample: sample
      },
      success: function ( result ) {
          alert( 'Success' );
          alert( result );
      }
  } );
} //submit();

添加表控制器:

public function addTable(){
  $sample = json_decode(Input::get('pSample'));
  $sample1 = $sample[0];
  $sample2 = $sample[1];
  $sample3 = $sample[2];

  print_r($sample3);
  print_r($sample);
}

提前谢谢你。

【问题讨论】:

  • 500 (Internal Server Error) 提示您检查服务器的错误日志。
  • 在 jQuery 可用时使用它:samp[2] = $("#sample2").val(); 也永远不要调用任何提交 - 您可能想要使用实际表单的提交事件并遇到问题

标签: php jquery laravel-5.4


【解决方案1】:

试试这个代码:

<meta name="csrf_token" content="{{ csrf_token() }}" />

然后,你需要发出一个 ajax 请求,使用 beforeSend 方法。

$("#try").click(function(){
    var url = $(this).attr("data-link");
    $.ajax({
        url: "test",
        type:"POST",
        beforeSend: function (xhr) {
            var token = $('meta[name="csrf_token"]').attr('content');
            if (token) {
              return xhr.setRequestHeader('X-CSRF-TOKEN', token);
            }
        },
        data: { testdata : 'testdatacontent' },
        success:function(data){
            alert(data);
        },error:function(){ 
            alert("error!!!!");
        }
    }); //end of ajax
});

控制器:

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;

class SampleController extends Controller
{
    /**
     * Receive data here
     *
     * @param  Request  $request
     * @return Response
     */
    public function addTable(Request $request) { 
        if ($request->isMethod('post')) {
            //$request->input('field_name_here');
            $sample = $request->input('pSample');
            print_r($sample); 
        }
    }
}
?>

【讨论】:

  • 我将您的 beforeSend 复制到我的 ajax 并将您的元数据复制到我的标题,但我仍然收到错误。
  • 你尝试像这样简单的回显并再次检查:public function addTable(){ echo "Wroking"; }
  • 在控制器中使用此代码:public function addTable(Request $request) { if ($request-&gt;isMethod('post')) { $sample = $request-&gt;input('pSample'); print_r($sample); } }
  • 你还需要包含命名空间:use Illuminate\Http\Request;
  • Input::get('field'); 在 laravel 5.4 中不可用。
【解决方案2】:
<meta name="csrf-token" content="{{ csrf_token() }}">

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

csrf-x-csrf-token

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-24
    • 2020-10-01
    • 2018-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多