admin管理员组文章数量:1252793
I read the documentation on axios on PUT request and it seems similar to a GET request. However, there wasn't an example code like GET, but I assume it is similar to how to do a GET request. I seem to have issue making a PUT request using axios. This is what I have so far with a test server that I am using:
axios.put('http://localhost:8080/cats')
.then(res => {
this.setState({
cat: res
});
})
.catch((err) => {
console.log(err);
})
Basically, I am trying to select an item and make changes to it.
I read the documentation on axios on PUT request and it seems similar to a GET request. However, there wasn't an example code like GET, but I assume it is similar to how to do a GET request. I seem to have issue making a PUT request using axios. This is what I have so far with a test server that I am using:
axios.put('http://localhost:8080/cats')
.then(res => {
this.setState({
cat: res
});
})
.catch((err) => {
console.log(err);
})
Basically, I am trying to select an item and make changes to it.
Share Improve this question edited Apr 7, 2021 at 8:08 frederj 1,7211 gold badge11 silver badges20 bronze badges asked May 22, 2017 at 0:47 jannjann 671 gold badge1 silver badge2 bronze badges 3-
1
with a
PUT
, one would expect some data to put ...I seem to have issue
what is the issue? and error? a warning? your dog ran away? – Jaromanda X Commented May 22, 2017 at 0:48 - Wele to Stack Overflow, please read about How to Ask a Question and How to create a Minimal, Complete, and Verifiable example in this site. Always be specific and make your best effort before asking. In this case, you don't mention the error so we can not know what is wrong. – Teocci Commented May 22, 2017 at 1:03
-
1
BTW your strurcture must be like this
// Send a PUT request axios({ method: 'put', url: 'http://localhost:8080/cats', data: { data: 'some data here' } });
Check this web for more info axios – Teocci Commented May 22, 2017 at 1:04
2 Answers
Reset to default 23I think you don't understand how Axios and HTTP requests work. When making a PUT request, you have to send the data you want to "put" into the item. You seem to think that when you make a PUT request you will receive the item back which you can then edit and have it save automatically, which is not true.
Image that you had a whole bunch of cats, and they had names, images, and descriptions about them. Now say you want to update the name of the cat which is identified by the number 1 (that's its ID).
Here is an example of using a PUT request (through Axios) to update that cat's name:
axios.put('https://example./cats/1', {
name: 'Tophat Cat'
})
.then(response => {
console.log(response);
})
.catch(error => {
console.log(err);
});
See how I had to specify which cat I wanted to update (by providing the unique URL for that cat) and also supply the specific data I want updated (the name)? Now it would be up to your server to see that it received a PUT request for the first cat, and that the data says to update the name to "Tophat Cat".
Then the server would send a response (it could be anything from "The update was successful." to a JSON representation of the updated cat's data.
Remember, a PUT request should only be used for updating data that already exists. If you want to add new data (which is what it looks like a little bit in your example, since you were pointing the request to just /cats
without an ID), you should instead use a POST request. POST requests are intended for adding new data and PUT requests are intended for updating existing data.
The POST request would look very similar to the PUT request example above, but with some important changes:
axios.post('https://example./cats', {
name: 'Catsandra',
image: 'https://example./images/catsandra.jpg',
description: 'Catsandra is the fanciest cat in town!'
})
.then(response => {
console.log(response);
})
.catch(error => {
console.log(err);
});
Now the request is going to just /cats
which is typical in a REST API (it can't be pointed at a specific cat ID because the cat doesn't exist yet). It also specifies all the data needed to create a new cat. In the PUT request we only included what we wanted to update. In a POST request the other data doesn't exist yet so we have to specify everything.
import { Axios } from 'axios';
Axios.put('http://localhost:8080/cats')
.then(res => {
this.setState({
cat: res
});
})
.catch((err) => {
console.log(err);
})
本文标签: javascriptAxios PUT request to serverStack Overflow
版权声明:本文标题:javascript - Axios PUT request to server - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740703944a2279614.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论