从vue到laravel的axios put/patch请求错误405

gpfsuwkq  于 7个月前  发布在  iOS
关注(0)|答案(2)|浏览(108)

帮助我,我不能得到更新方法与put/patch一起工作。当它是react时也没有问题,所以也许vue有什么问题,或者也许我错过了简单的东西。

let formData = new FormData();
            formData.append("image", this.image);
            formData.append("name", this.name);
            axios
                .post(
                    "/api/items/" + this.editId,
                    {
                        _method: 'patch',
                        data: formData,
                    },
                    {
                        headers: {
                            "Content-Type": "multipart/form-data"
                        }
                    }
                )
                .then(function(response) {
                    console.log(response.data);
                })

字符串
我也试

axios.put(
                    "/api/items/" + this.editId,
                    {
                        data: formData,
                    },
                    {
                        headers: {
                            "Content-Type": "multipart/form-data"
                        }
                    }
                )


但后来我在laravel的控制器中的update方法中得到了空的$request(id工作)
这是我在api.php中定义路由的方式

Route::apiResource('items', 'API\ItemsController');

gkn4icbw

gkn4icbw1#

试试这个

let formData = new FormData();
            formData.append("image", this.image);
            formData.append("name", this.name);
     axios.
           put("/api/items/" + this.editId, {
                  params: {
                     data: formData
                },

                  headers: {
                       "Content-Type": "multipart/form-data"
                }

            })
            .then(response => {
                  console.log(response);
            })
            .catch(error => {
                console.log(error);
            });

字符串

hm2xizp9

hm2xizp92#

使用

{ 'X-HTTP-Method-Override': 'PATCH' }

字符串
指定你想要的方法。上面代码的例子;

let formData = new FormData();
        formData.append("image", this.image);
        formData.append("name", this.name);
        axios
            .patch(
                "/api/items/" + this.editId,
                {
                    data: formData,
                },
                {
                    headers: {
                        "Content-Type": "multipart/form-data",
                        "X-HTTP-Method-Override": "PATCH"
                    }
                }
            )
            .then(function(response) {
                console.log(response.data);
            })

相关问题