【问题标题】:how to get axios.post(`api/ImageUpload`, img_mode) value from $request in laravel controller如何从 laravel 控制器中的 $request 获取 axios.post(`api/ImageUpload`, img_mode) 值
【发布时间】:2020-02-21 20:31:02
【问题描述】:

我完全是 Laravel 的初学者,所以请先生帮助我。

我将发送Image_Mode="Gallery" 来自 反应 axios.post() 并想在 laravel 控制器中获取 Image_Mode 值。

示例:

$img_mode = $request->input('img_mode'); 

反应代码

fileUploadHandler = (e) =>{ 让 img_mode =''; const config = { headers: { 'Content-Type': 'multipart/form-data' } , img_mode:this.state.img_mode};

    let formData = new FormData();
        formData.append("file", this.state.selectedFile);
    axios.post(`http://127.0.0.1:8000/api/ImageUpload`, formData,config,img_mode)
    .then(res => {
      console.log(res);
      console.log(res.data);
    })

  }

【问题讨论】:

  • dd($request->all()); ?
  • 你能展示一下你用来发送ajax的js代码吗?
  • 先生,我将附上输出屏幕截图。 img_mode:"Gallery" 获取并且我想在我的控制器中使用 img_mode 值
  • fileUploadHandler = (e) =>{ let img_mode =''; const config = { headers: { 'Content-Type': 'multipart/form-data' } , img_mode:this.state.img_mode};让 formData = new FormData(); formData.append("文件", this.state.selectedFile); axios.post(http://127.0.0.1:8000/api/ImageUpload, formData,config,img_mode) .then(res => { console.log(res); console.log(res.data); }) }

标签: php reactjs laravel request


【解决方案1】:

如果你像下面这样添加formData.append('img_mode', img_mode);,那么你的控制器代码应该从那里开始工作:

   fileUploadHandler = (e) => {
        let img_mode = '';
        const config = {
            headers: {
                'Content-Type': 'multipart/form-data'
            },
            img_mode: this.state.img_mode
        };
        let formData = new FormData();
        formData.append('img_mode', img_mode);
        formData.append("file", this.state.selectedFile);
        axios.post('http://127.0.0.1:8000/api/ImageUpload', formData,config,img_mode) .then(res => { console.log(res); console.log(res.data); }) 
    };

【讨论】:

  • 先生,非常感谢您的支持,我如何在控制器中获取 img_mode 值,我尝试了这个并得到 500 错误..... $img_mode=$request->file('img_mode') ; $img_modeName = $img_mode->getClientOriginalName();返回 $img_modeName;
猜你喜欢
  • 2013-07-30
  • 2021-06-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-25
  • 2014-07-01
  • 2017-09-09
  • 2017-08-08
相关资源
最近更新 更多