简体   繁体   English

Axios PUT 请求不起作用

[英]Axios PUT request doesn't work

axios.put method doesn't work while axios.post works. axios.put 方法在 axios.post 工作时不起作用。

This is working example for post request.这是发布请求的工作示例。 (example) (例子)

let response = await axios.post(`${ROOT_URL}/urls/url/`, {
    post_id,
    password,
    content
  }, { headers });
  console.log(response.status) // 201.

I just copied and pasted the valid post request, and modified some fields and method for put request.我只是复制并粘贴了有效的 post 请求,并修改了put请求的一些字段和方法。 But it returns 400 error on server side.但它在服务器端返回 400 错误。

let response = await axios.put(`${ROOT_URL}/profile/update/`, {
    title,
    gender
  }, { headers }); <- Server prints 400 HTTP error.

I tested with Postman and I confirmed that it works with put method.我用Postman进行了测试,并确认它适用于 put 方法。 I have to think that my syntax for axios.put is wrong but I'm not sure how it can be different with the post method.我不得不认为我的 axios.put 语法是错误的,但我不确定它与 post 方法有何不同。

If you see axios's official doc page, it looks almost identical.如果你看到 axios 的官方文档页面,它看起来几乎一模一样。 Axios documentation link Axios 文档链接

And axios version is 0.16.2 : "axios": "^0.16.2",而 axios 版本是 0.16.2 : "axios": "^0.16.2",

400 is bad request not 405 method not allowed. 400 是错误的请求,而不是 405 方法不允许。

I'd check what your posting is correct.我会检查您发布的内容是否正确。

Is this a valid object?这是一个有效的对象吗?

{
    title,
    gender
}

Example:例子:

axios.put('/api/something', {
    foo: bar
})
    .then(function (response) {
        // do something...
    }.bind(this))
    .catch(function (error) {
        console.log(error)
    });

I post this here maybe it answers someone's problem我在这里发布这个也许它回答了某人的问题

if in your api setup in the backend.如果在后端的 api 设置中。 the route is configured like this 'api/user1 or some other mode of identification/someData/anotherData'路由配置为这样的'api/user1 or some other mode of identification/someData/anotherData'

you should send the data exactly as such and not as an object.您应该完全按原样发送数据,而不是作为对象发送。 so in my case it was like this: axios.put(`${Routes.ConfrimCode}${phoneNum}/${imei}/${code}`).then(res=>{ //callback })所以在我的情况下是这样的: axios.put(`${Routes.ConfrimCode}${phoneNum}/${imei}/${code}`).then(res=>{ //callback })

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

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