Backbone.save POST instead of PUT

让人想犯罪 __ 提交于 2019-12-19 18:48:32

问题


just a short question:

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.

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


回答1:


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.

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

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

  1. PUT vs POST in REST
  2. RESTful web services on Wikipedia

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

  • saving a new model causes a change in the system, a new URL is added, the action is not idempotent, it should be a POST,
  • saving a known model replaces a resource at a given URL, the action is idempotent, it should be a PUT.


来源:https://stackoverflow.com/questions/11013049/backbone-save-post-instead-of-put

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!