Axios PUT 请求到服务器 [英] Axios PUT request to server
问题描述
我阅读了有关 PUT 请求的 axios 文档,它似乎类似于 GET 请求.但是,没有像 GET 这样的示例代码,但我认为它类似于如何执行 GET 请求.我似乎在使用 axios 发出 PUT 请求时遇到问题.到目前为止,我使用的测试服务器是这样的:
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.
推荐答案
我想你不明白 Axios 和 HTTP 请求是如何工作的.发出 PUT 请求时,您必须发送要放入"项目的数据.您似乎认为,当您发出 PUT 请求时,您会收到返回的项目,然后您可以对其进行编辑并自动保存,但事实并非如此.
I 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.
你有一大群猫的图片,它们有关于它们的名字、图像和描述.现在假设您要更新由数字 1(即它的 ID)标识的猫的名称.
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).
下面是一个使用 PUT 请求(通过 Axios)来更新那只猫的名字的例子:
Here is an example of using a PUT request (through Axios) to update that cat's name:
axios.put('https://example.com/cats/1', {
name: 'Tophat Cat'
})
.then(response => {
console.log(response);
})
.catch(error => {
console.log(err);
});
看看我如何指定我想要更新的猫(通过提供那只猫的唯一 URL)并提供我想要更新的特定数据(名称)?现在由您的服务器来查看它是否收到了对第一只猫的 PUT 请求,并且数据表明将名称更新为Tophat Cat".
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".
然后服务器会发送一个响应(它可以是任何东西,从更新成功."到更新后的猫数据的 JSON 表示.
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.
请记住,PUT 请求只能用于更新已经存在的数据.如果您想添加新数据(这在您的示例中看起来有点像,因为您将请求指向没有 ID 的 /cats
),您应该使用 POST 请求.POST 请求用于添加新数据,PUT 请求用于更新现有数据.
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.
POST 请求看起来与上面的 PUT 请求示例非常相似,但有一些重要的变化:
The POST request would look very similar to the PUT request example above, but with some important changes:
axios.post('https://example.com/cats', {
name: 'Catsandra',
image: 'https://example.com/images/catsandra.jpg',
description: 'Catsandra is the fanciest cat in town!'
})
.then(response => {
console.log(response);
})
.catch(error => {
console.log(err);
});
现在请求只会发送到 /cats
这在 REST API 中很典型(它不能指向特定的猫 ID,因为猫还不存在).它还指定所有创建新猫所需的数据.在 PUT 请求中,我们只包含了我们想要更新的内容.在 POST 请求中,其他数据尚不存在,因此我们必须指定所有内容.
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.
这篇关于Axios PUT 请求到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!