不,这行不通。
你必须传递给 Axios 的是一个请求配置对象,在这个对象中你可以找到data
和params
属性。 您将使用它们将数据发送到服务器。
这样: axios.delete(URL, {params: {foo: 'bar'}})
在delete request
中,您应该使用params
而不是data
,请参阅文档:
// "data" is the data to be sent as the request body
// Only applicable for request methods 'PUT', 'POST', and 'PATCH'
// When no
转换请求is set, must be of one of the following types:
// - string, plain object, ArrayBuffer, ArrayBufferView, URLSearchParams
// - Browser only: FormData, File, Blob
// - Node only: Stream
data: {
firstName: 'Fred'
},
@charlesrochati我很困惑。 params
是发送查询字符串参数,这不是我正在做的。
至于您上面引用的内容,它没有说明delete
。
params 是发送查询字符串参数,这不是我正在做的。
如果您想制作axios.delete
,那正是您必须做的。
你不使用axios.delete(URL, {
data: { foo: 'bar' }
//仅适用于请求方法'PUT'、'POST'和'PATCH'
})
对于删除请求,您将使用axios.delete(URL, {
params: { foo: 'bar' }
})
仅当您使用“PUT”、“POST”和“PATCH”时,您才会将数据作为请求正文发送。
恐怕您正在使用axios.delete
就像axios.post
或( axios.put
或axios.patch
)。 使用axios.delete
时,您将向服务器发送id
,服务器本身会找到与给定id
resource
然后它将被删除(或触发某些操作,但请求被映射为http delete
)。
通过使用axios.delete(URL, {
data: { foo: 'bar' }
})
您正在发送resource
,这根本没有意义。
看看下面的链接,你会有一个更好的理解:
https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html [第 9.7 节]
HTTP DELETE 方法不应有请求正文。 DELETE 告诉源服务器删除由 URI 标识的资源。
就我而言,我也提供了一个有效载荷。 但无论哪种方式,如果它使用data
工作,它应该使用速记工作。
编辑:规范不允许/禁止传递有效负载。
axios.delete 没有简写:
axios.request(配置)
axios.get(url[, 配置])
axios.delete(url[, 配置])
axios.head(url[, 配置])
axios.post(url[, data[, config]])
axios.put(url[, data[, config]])
axios.patch(url[, data[, config]])
@AkiraLaine规范也不禁止发送 GET 请求正文。 axios 是否也应该允许您在 GET 请求中使用简写符号来发送请求正文?
我正在关闭这个问题,因为它是预期的行为。
@cggaurav它受支持。
axios.delete('/some/uri', { body: 'my delete body' })
附带说明一下,在服务器上,像这样使用 axios.delete('/some/uri', {params: { id: 1 } }) 时,id 显示在 req.query 而不是 req.params 中。 只是想把它扔出去。
@aricsangchat这可能是因为您使用的 express 根本不属于 axios。 当 express 引用params
时,它指的是请求路径中匹配的动态值。 当 axios 引用params
时,它指的是查询字符串参数。
// Express
router.get('/:foo/:bar', (req, res) => {
res.json({
query: req.query,
params: req.params
})
})
// Axios
axios.get('/some/uri', { params: { id: 1 } })
.then((response) => console.log(response.data))
您从运行中获得的输出将是:
{
"query": { "id": 1 },
"params": {
"foo": "some",
"bar": "uri"
}
}
@jcready 是的,这就是我们的设置。
哪里写删除不能有正文(如帖子)? 如果 axios 支持这个就好了https://tools.ietf.org/html/draft-ietf-httpbis-p2-semantics-19#page -23
谢谢
供参考。 公平点 DELETE 不应该有正文。 但是,有时当 api 超出您的控制范围时,您不得不发送正文。 例如
https://auth0.com/docs/api/authorization-extension#remove -user-from-roles 👎
@duhseekoh
const extension_url = 'mycompany.auth0.com'
const access_token = 'abc123'
const user_id = 12345
const role_id = 67890
axios.delete(`https://${extension_url}/users/${role_id}/roles`, {
headers: {
'Authorization': `Bearer ${access_token}`,
'Content-Type': 'application/json'
},
body: JSON.stringify([
user_id
])
})
@jcready - 当然,感谢这个例子。 只是想添加一些推理,说明为什么在使用 axios 时可能需要在 DELETE 上传递一个主体。
@duhseekoh也许这会有所帮助: https ://github.com/axios/axios/issues/897#issuecomment -343715381
const response = await axios.delete('/api/matches/delete_match', {
data: { matchInfo }
});
我让它与data
一起使用,但不与params
与版本"axios": "^0.17.1"
一起使用
@quinnliu我认为选择使用data
或params
取决于您的后端如何处理请求。
data
是将有效负载放入请求正文中,就像使用post
一样。params
是将有效负载视为 url 参数。 就像你发送axios.delete(URL, {
params: { foo: 'bar' }
})
与向URL?foo=bar.
发送请求相同
api 中 axios 的抽象。 数据中的参数:仅名称
``
api.delete(
/projects/${idProjectTech}/techs`, {data: { name: data.name }})
最有用的评论
不,这行不通。
你必须传递给 Axios 的是一个请求配置对象,在这个对象中你可以找到
data
和params
属性。 您将使用它们将数据发送到服务器。这样:
axios.delete(URL, {params: {foo: 'bar'}})
在
delete request
中,您应该使用params
而不是data
,请参阅文档:// "data" is the data to be sent as the request body
// Only applicable for request methods 'PUT', 'POST', and 'PATCH'
// When no
转换请求is set, must be of one of the following types:
// - string, plain object, ArrayBuffer, ArrayBufferView, URLSearchParams
// - Browser only: FormData, File, Blob
// - Node only: Stream
data: { firstName: 'Fred' },