2

我有一个 SPA(单页应用程序),因此它广泛使用 AJAX 来获取和保存与服务器之间的数据。在一种情况下,我允许管理员查看/添加/编辑/删除用户。该区域的一些当前 url 看起来像:

(GET) /users?userId=1   // get user with id of 1
(POST) /users?userId=1&firstName=Jim    // update the first name of the user with id 1
(POST) /users?firstName=Bob    // create a new use with the first name Bob
(POST) /users?userId=1&delete=true    // delete user with id of 1

在相关项目中花了一些时间在 RESTful API 上工作后,我想知道是否更喜欢在 Web 应用程序中使用 HTTP 类型(GET、POST、PUT、DELETE)。另外,使用用户 ID 的路径参数而不是查询参数更好吗?从长远来看,这些网址(重写上述网址)是否是更好的选择:

(GET) /users/1   // get user with id of 1
(PUT) /users/1?firstName=Jim    // update the first name of the user with id 1
(POST) /users?firstName=Bob    // create a new use with the first name Bob
(DELETE) /users/1    // delete user with id of 1
4

1 回答 1

1

理论上是的,你应该。您应该尽可能地使用 RESTful,这意味着充分利用 HTTP 语义。然而现实有点模糊,几个旧的浏览器,我不需要命名,不支持除了GETand之外的任何东西POST。因此,在这些浏览器停止支持,或者直到您放弃对这些浏览器的支持之前,当前的建议是使用备份方法来做同样的事情,但 on POST,通常在 url 中有一个额外的参数或段。

于 2013-06-29T16:57:09.633 回答