Javascript Axios PUT 请求到服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44103187/
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-08-23 02:20:38  来源:igfitidea点击:

Axios PUT request to server

javascriptreactjsaxios

提问by jann

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:

我在 PUT 请求上阅读了有关 axios 的文档,它似乎类似于 GET 请求。但是,没有像 GET 这样的示例代码,但我认为它类似于如何执行 GET 请求。我似乎在使用 axios 发出 PUT 请求时遇到问题。到目前为止,我使用的测试服务器是这样的:

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.

基本上,我正在尝试选择一个项目并对其进行更改。

回答by Nathan Heffley

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.

我想你不明白 Axios 和 HTTP 请求是如何工作的。发出 PUT 请求时,您必须发送要“放入”项目的数据。您似乎认为,当您发出 PUT 请求时,您会收到返回的项目,然后您可以对其进行编辑并自动保存,但事实并非如此。

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).

想象你有一大群猫,它们有关于它们的名字、图像和描述。现在假设您要更新由数字 1(即它的 ID)标识的猫的名称。

Here is an example of using a PUT request (through Axios) to update that cat's name:

下面是一个使用 PUT 请求(通过 Axios)来更新那只猫的名字的例子:

axios.put('https://example.com/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".

看看我如何指定我想要更新的猫(通过提供那只猫的唯一 URL)并提供我想要更新的特定数据(名称)?现在由您的服务器来查看它是否收到了对第一只猫的 PUT 请求,并且数据表明要将名称更新为“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.

然后服务器会发送一个响应(它可以是从“更新成功”到更新后的猫数据的 JSON 表示的任何内容。

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 /catswithout an ID), you should instead use a POST request. POST requests are intended for adding new dataand PUT requests are intended for updating existing data.

请记住,PUT 请求只能用于更新已经存在的数据。如果您想添加新数据(这在您的示例中看起来有点像,因为您只是在/cats没有 ID 的情况下将请求指向),您应该改用 POST 请求。POST 请求用于添加新数据,PUT 请求用于更新现有数据

The POST request would look very similar to the PUT request example above, but with some important changes:

POST 请求看起来与上面的 PUT 请求示例非常相似,但有一些重要的变化:

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

Now the request is going to just /catswhich 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 allthe 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.

现在请求将发送到/catsREST API 中的典型请求(它不能指向特定的猫 ID,因为猫尚不存在)。它还指定了创建新猫所需的所有数据。在 PUT 请求中,我们只包含了我们想要更新的内容。在 POST 请求中,其他数据尚不存在,因此我们必须指定所有内容。