REST 作为数据接口有意义吗?
我们正在为移动设备设计一个数据驱动的 Web 应用程序,使用 jQuery Mobile 作为界面。使用 RESTful 接口来获取数据有意义吗?我可以看到拥有一个 RESTful 路径的网站有多么有用,因为用户可以轻松地操纵路径和交叉导航,但如果 AJAX 调用对用户来说都是隐藏的,那么 REST 是否会带来任何好处(除了让 API 易于其他开发人员理解之外)。
我之所以问这个问题,是因为我不相信 REST 能正确使用 HTTP。例如在以下示例中: 1./产品/12345/ 2./产品?id=12345 选项 1 更加 RESTful,但选项 2 更加“符合 HTTP”,因为这就是标准浏览器提交的方式。在 jQuery 中,您甚至可以将表单的元素序列化为 AJAX 请求,结果是查询字符串而不是路径。要获取 REST URL,您始终需要操作标准 URL 格式,这是额外的代码,没有任何实际好处。 REST 是否完全忽略查询字符串,或者它们仅用于查询(即搜索)?
We're designing a data driven web app for mobile devices using jQuery Mobile for the interface. Does it make sense to use a RESTful interface to fetch the data? I can see how it's useful to have a website whose paths are RESTful because the user can easily manipulate the path and cross-navigate but does a REST bring any benefit if the AJAX calls are all hidden from the user (aside from making the API easy for other developers to understand).
The reason I ask is because I am not convinced that REST properly uses HTTP. For example in the following examples:
1. /product/12345/
2. /product?id=12345
Option 1 is more RESTful but 2 is more HTTP "compliant" because that's how s are submitted by standard browsers. In jQuery you can even serialise a form's elements into an AJAX request and the result is a query string and not a path. To get REST URLs you would always need to manipulate standard URL formations which is extra code for no real benefit. Does REST completely ignore query strings or are they only for queries i.e. searching?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
恕我直言,就 REST 而言,绝对没有区别。在 RFC 2396 中,有一行
后来在 RFC 3986 为:
所以两者现在是等价的,但正如你说
“除了让 API 易于其他开发人员理解之外”
您肯定不会掉以轻心,它应该是您编写的任何代码行中最重要的事情
IMHO as far as REST is concerned there is absolutely no difference. In RFC 2396 there was a line
which was was later changed in RFC 3986 to be:
So both are equivalent now but as you said
"aside from making the API easy for other developers to understand"
You sure don't mean to take this lightly, it should be the most important thing in any line of code you ever write
REST 标识资源的位置,而不是该资源的属性。因此,当您向 REST API 提交对对象的更改时,您可以发布它们或使用查询字符串。对客户端来说没有太多实际好处,但它确实使后端更易于测试和管理。
REST identifies the location of a resource - not the properties of that resource. So when you're submitting changes to an object to a REST API you can either post them or use a query string. There's not much of a practical benefit on the client, but it certainly makes the backend easier to test and manage.