简体   繁体   English

与RESTful服务和Ajax混淆

[英]Confused with RESTful service and ajax

I'm not a good english speaker, give me mercy about the bad explanation 我的英语说得不好,请给我怜悯错误的解释

As developing web, i am always confused about the RESTful's definition. 在开发Web时,我总是对RESTful的定义感到困惑。

OK, for instance, the client(ie browser, mobile or etc) wants a user list 好的,例如,客户端(例如浏览器,移动设备等)需要用户列表

so the client request a GET method url. 因此客户端请求GET方法网址。

domain/give_me_user_list

gotcha! 疑难杂症! the server responses a json format data like this. 服务器响应这样的json格式数据。

{
    "users": [
        {
            "name": "john doe"
        },
        {
            "name": "john smith"
        }
    ]
}

Is this RESTful service? 这是RESTful服务吗? that's all? 就这样?

But what about ajax call? 但是ajax调用呢?

$.ajax ->
 url: domain/give_me_user_list,
 success: (users) ->
  #do somthing

Do we call this RESTful service too? 我们也将其称为RESTful服务吗?

My question is, RESTful service term sounds like a trendy skill made new, but it my feeling it is just an ajax call... 我的问题是,RESTful服务术语听起来像是一种新的时髦技能,但是我感觉这只是一个ajax调用...

They're really two completely different things which are frequently used together. 它们实际上是两个完全不同的东西,经常一起使用。

RESTful Services RESTful服务

RESTful describes a way for a service to define its API. RESTful描述了一种服务定义其API的方法。 It favors having a single URL per type of request, and using the native action methods built into HTTP. 它倾向于每种请求类型使用一个URL,并使用HTTP内置的本机操作方法。 Contrast this with a another common pattern where all requests go to the same URL using the POST method, and the description of the desired resource and action are encoded within the message body. 与此形成对比的另一种常见模式是,所有请求都使用POST方法转到相同的URL,并且在消息正文中对所需资源和操作的描述进行了编码。

So, let's consider an example of each approach. 因此,让我们考虑每种方法的示例。 Let's say we have a typical blog which is made up of posts with related comments. 假设我们有一个典型的博客,该博客由带有相关评论的帖子组成。 Using the older non-RESTful style, you might make the following request to read a specific blog post, and then to write some change: 使用较旧的非RESTful样式,您可以发出以下请求以阅读特定的博客文章,然后进行一些更改:

POST /service/action.do
    body: { "action":"read", "resource":"blog_post", "id":1234 }

POST /service/action.do
    body: {"action":"update", "resource":"blog_post", "id":1234, text:"Lorem ipsum..." }

With a RESTful service, it would probably look something more like: 有了RESTful服务,它看起来可能会更像:

GET /service/blog_post/1234
    body: none

PUT /service/blog_post/1234
    body: { "text":"Lorem ipsum..."}

There's lots more to it, and some of it pretty subtle, but that's the main difference between a RESTful and non-RESTful service API. 它还有很多,而且有些还很微妙,但这是RESTful和非RESTful服务API之间的主要区别。

AJAX AJAX

AJAX describes an approach to building web pages where new content can be added to the web page without needing to completely refresh it from the server. AJAX描述了一种构建网页的方法,其中可以将新内容添加到网页中,而无需从服务器中完全刷新它。 A small example of this is a finance page which updates the stock price without a page refresh, and a large example is something like Google Maps where you see no full-page refreshes at all. 一个小例子是财务页面,该页面无需更改页面即可更新股价,而大例子是Google Maps这样的页面,您根本看不到全页面刷新。

In both cases, as the user interacts with the page, new content is loaded by using the XMLHTTPRequest object provided as part of the JavaScript environment in the browser. 在这两种情况下,当用户与页面交互时,都将使用XMLHTTPRequest对象来加载新内容,该XMLHTTPRequest对象是浏览器中JavaScript环境的一部分。 This allows the JavaScript code to make regular HTTP requests without triggering the browser to refresh the page. 这允许JavaScript代码发出常规HTTP请求,而无需触发浏览器刷新页面。 Once a response is returned, more JavaScript code is used to integrate the new data into the existing page. 返回响应后,将使用更多JavaScript代码将新数据集成到现有页面中。

Summary 摘要

As you can see, the two refer to different things altogether, but can readily be used together to make a dynamic website. 如您所见,两者完全是指不同的事物,但是可以很容易地一起使用,以创建一个动态网站。 The AJAX part refers to how the client is put together, while the RESTful part refers to how the server is put together. AJAX部分是指如何将客户端组合在一起,而RESTful部分是指如何将服务器组合在一起。

See also: AJAX , RESTful 另请参阅: AJAXRESTful

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM