Php 请求对象在lumen api中为空

Php 请求对象在lumen api中为空,php,ajax,laravel,api,lumen,Php,Ajax,Laravel,Api,Lumen,使用ajax调用在lumen API中上载图像文件,无法获取lumen中的图像进行处理,请求对象为空 Ajax调用 var file = $('#img')[0].files[0]; var form_data = new FormData(document.getElementById("myform")); form_data.append("img", file); $.ajax({ url: "http://localhost:8000/api/image", type

使用ajax调用在lumen API中上载图像文件,无法获取lumen中的图像进行处理,请求对象为空

Ajax调用

var file = $('#img')[0].files[0];
var form_data = new FormData(document.getElementById("myform"));
form_data.append("img", file);

$.ajax({
    url: "http://localhost:8000/api/image",
    type: "POST",
    data: form_data,
    enctype: 'multipart/form-data',
    processData: false, // tell jQuery not to process the data
    contentType: false   // tell jQuery not to set contentType
})
.done(function (data) {
    console.log(data);
});
路线

$api->post('/image', 'ImagesController@addImage');
控制器

public function addImage(Request $request) {
    return $request; // returns empty object
}


在控制器中添加此行

use Illuminate\Http\Request;

不要使用
请求
外观,因为Lumen的工作方式与Laravel不同。

您可以通过这种方式构建JavaScript对象,这将确保将主体传递给请求

let form_data = {
      'name': $('#name').value(),
      'file': $('#img')[0].files[0]
    };

    $.ajax({
      url: "http://localhost:8000/api/image",
      type: "POST",
      data: form_data,
      enctype: 'multipart/form-data',
      processData: false, // tell jQuery not to process the data
      contentType: false   // tell jQuery not to set contentType
    })
    .done(function (data) {
      console.log(data);
    });

你把它修好了吗?