Backbone.save POST 而不是 PUT [英] Backbone.save POST instead of PUT

查看:22
本文介绍了Backbone.save POST 而不是 PUT的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是一个简短的问题:

拥有模型的新实例并发出 model.save() 并将 URL 设置为 /api/store/category,Backbone 发出 POST.据我所知,它应该使用 PUT,就像在这个 "PUT 或 POST:故事的其余部分" 博客文章.

Having the new instance of a model and issuing a model.save() with URL set to /api/store/category, Backbone issues a POST. According to my knowledge, it should use PUT, like mentioned in this "PUT or POST: The REST of the Story" blog post.

谁是对的?BB 还是这篇文章的作者?

Who is right? BB or this article's author?

推荐答案

根据 Backbone 文档, 保存新模型将产生 POST 请求,保存现有模型(具有 id)将发出 PUT 请求.

According to Backbone documentation, saving a new model will result in a POST request, and saving an existing model (having an id) will emit a PUT request.

保存 model.save([attributes], [options])
...
如果模型是新的,则保存将是创建"(HTTP POST),如果模型已经存在于服务器上,保存将是更新"(HTTP PUT).

save model.save([attributes], [options])
...
If the model isNew, the save will be a "create" (HTTP POST), if the model already exists on the server, the save will be an "update" (HTTP PUT).

如果您想知道 Backbone 是否应该使用 POST 进行创建,请检查

And if you are wondering if Backbone should use a POST for creation, check

  1. REST 中的 PUT 与 POST
  2. RESTful 网络服务 维基百科

根据这些文章,我想说的是,在 Backbone 的上下文中,动词的使用是正确的:

In the light of these articles, I'd say that, in the context of Backbone, the verbs are correctly used:

  • 保存一个新模型会导致系统发生变化,添加了一个新的 URL,动作不是幂等的,应该是一个 POST,
  • 保存已知模型会替换给定 URL 处的资源,该操作是幂等的,应该是 PUT.

这篇关于Backbone.save POST 而不是 PUT的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆