简体   繁体   English

Axios 补丁请求不适用于 Laravel

[英]Axios patch request not working with Laravel

I'm trying to make a patch request from axios, but the data is not being sent.我正在尝试从 axios 发出补丁请求,但未发送数据。

const url = route('industries::update',id)

const headers = {
    'Content-Type': 'application/x-www-form-urlencoded'
}

return axios.patch(url,data,{ headers })

the data parameter is "FormData()"数据参数是“FormData()”

I've also tried appending the key method with _PATCH in the form data我还尝试在表单数据中使用_PATCH附加关键方法

let formData = new FormData()
formData.append('method','_PATCH')

But nothing seems to work.但似乎没有任何效果。 I get the default laravel's 422 error from the responses (that the values are required).我从响应中得到默认的 laravel 422 错误(需要这些值)。

1st.第一。 Your data is an instance of FormData but your header is application/x-www-form-urlencoded which is wrong, use multipart/form-data instead.您的数据是FormData一个实例,但您的标题是application/x-www-form-urlencoded这是错误的,请改用multipart/form-data However, it will be set automatically when you use instance of FormData as data.但是,当您使用FormData实例作为数据时,它将自动设置。

2nd.第二。 Send request via axios.post and append _method: PATCH to your formData :通过axios.post发送请求并将_method: PATCH附加到您的formData

const url = route('industries::update', id)

/*
const headers = {
    'Content-Type': ' multipart/form-data' // <= instead of `application/x-www-form-urlencoded`
}
*/

return axios.post(url, data) // <= instead of `axios.patch` and omit unnecessary `headers`

And:和:

let formData = new FormData()
formData.append('_method', 'PATCH') // <= instead of `('method', '_PATCH')`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM