javascript Axios PUT 请求不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47676179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 07:32:59  来源:igfitidea点击:

Axios PUT request doesn't work

javascriptreactjsreact-nativeaxios

提问by hellofanengineer

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 putrequest. But it returns 400 error on server side.

我只是复制粘贴了有效的post请求,并修改了一些put请求的字段和方法。但它在服务器端返回 400 错误。

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

I tested with Postmanand I confirmed that it works with put method. 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.

我测试Postman并确认它适用于 put 方法。我不得不认为我的 axios.put 语法是错误的,但我不确定它与 post 方法有何不同。

If you see axios's official doc page, it looks almost identical. Axios documentation link

如果你看到 axios 的官方文档页面,它看起来几乎一模一样。Axios 文档链接

And axios version is 0.16.2 : "axios": "^0.16.2",

而 axios 版本是 0.16.2 : "axios": "^0.16.2",

回答by Vince Lowe

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)
    });

回答by Alireza tk

I post this here maybe it answers someone's problem

我张贴在这里也许它回答了某人的问题

if in your api setup in the backend. the route is configured like this 'api/user1 or some other mode of identification/someData/anotherData'

如果在后端的 api 设置中。路由是这样配置的 '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 })