【问题标题】:Request input laravel ajax return null on update method请求输入 laravel ajax 在更新方法上返回 null
【发布时间】:2019-07-16 06:17:04
【问题描述】:

我尝试使用 put 方法使用 ajax 更新 laravel 中的数据,当我回显请求输入时结果为空,这是我的 ajax

let formData = new FormData();
formData.append('name', 'lorem ipsum'); // I want get this

$.ajax({
   url     : 'product/17',
   method  : 'put',
   data: formData,
   contentType: false,
   processData: false,
   headers: {
      'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
   },
   success : function(res){
      alert(res); // result is null
   }
});

路线:

Route::resource('/product', 'ProductController');

控制器:

public function update(Request $request, $id){
   echo $id; // result is 17
   echo $request->input('name'); // result is null :(
}

我一直在搜索并尝试其他代码但无法正常工作,如何解决?谢谢

【问题讨论】:

    标签: php ajax laravel


    【解决方案1】:

    我想可能是因为请求的方法。发送 put 请求的方式与普通的 get/post 请求不同。你可以这样修改:

    let formData = new FormData();
    formData.append('name', 'lorem ipsum'); // I want get this
    formData.append('_method', 'put'); // Specify method
    
    $.ajax({
       url     : 'product/17',
       method  : 'post',
       data: formData,
       contentType: false,
       processData: false,
       headers: {
          'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
       },
       success : function(res){
          alert(res); // result is null
       }
    });
    

    现在,您可以使用相同的方法访问数据。

    $request->input('name');
    

    希望你能理解。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-31
      • 2017-11-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-23
      • 2014-04-26
      • 2016-02-22
      相关资源
      最近更新 更多