我有一个 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