AJAX、RESTful/Rest、JSON 和 JSONP 之间有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7115940/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:57:35  来源:igfitidea点击:

What is the difference between AJAX, RESTful/Rest, JSON and JSONP?

jsonajaxrestjsonp

提问by Nathan Ratcliff

I am just confused with the these terms. Can anybody please provide/explain me brief with an example?

我只是对这些术语感到困惑。任何人都可以提供/解释我的例子吗?

回答by Nathan Ratcliff

  • Ajax- "Asynchronous Javascript and XML". Ajax loosely defines a set of technologies to help make web applications present a richer user experience. Data updating and refreshing of the screen is done asynchronously using javascript and xml (or json or just a normal http post).

  • JSON- "Javascript Object Notation". JSON is like xml in that it can be used to describe objects, but it's more compact and has the advantage of being actual javascript. An object expressed in JSON can be converted into an actual object to be manipulated in javascript code.

  • By default, Ajax requests have to occur in the same domain of the page where the request originates. JSONP- "JSON with padding" - was created to allow you to request JSON resources from a different domain. (CORSis a newer and better alternative to JSONP.)

  • REST- "Representational State Transfer". Applications using REST principles have a Url structure and a request/response pattern that revolve around the use of resources. In a pure model, the HTTP Verbs Get, Post, Put and Delete are used to retrieve, create, update and delete resources respectively. Put and Delete are often not used, leaving Get and Post to map to select (GET) and create, update and delete (POST)

  • Ajax- “异步 Javascript 和 XML”。Ajax 松散地定义了一组技术,以帮助使 Web 应用程序呈现更丰富的用户体验。屏幕的数据更新和刷新是使用 javascript 和 xml(或 json 或只是普通的 http 帖子)异步完成的。

  • JSON- “Javascript 对象表示法”。JSON 类似于 xml,因为它可以用来描述对象,但它更紧​​凑,并且具有作为实际 javascript 的优势。以 JSON 表示的对象可以转换为实际对象,以便在 javascript 代码中进行操作。

  • 默认情况下,Ajax 请求必须发生在请求发起的页面的同一域中。 JSONP- “带填充的 JSON” - 旨在允许您从不同的域请求 JSON 资源。(CORS是 JSONP 的更新更好的替代品。)

  • REST- “具象状态转移”。使用 REST 原则的应用程序具有 URL 结构和围绕资源使用的请求/响应模式。在纯模型中,HTTP Verbs Get、Post、Put 和 Delete 分别用于检索、创建、更新和删除资源。Put 和 Delete 经常不使用,让 Get 和 Post 映射到选择(GET)和创建、更新和删除(POST)

回答by aroth

Ajax, or more properly, AJAX, stands for Asynchronous Javascript And Xml. Technically it refers to any asynchronous request made by the browser (anything that uses an XmlHttpRequest) on behalf of some script running on the current page, regardless of what content-type is returned. It can also be used to describe a certain pattern of constructing a page/site where most/all of the content is fetched/updated dynamically on the page. When used to describe a data format, "ajax" typically means "xml".

Ajax,或者更准确地说,AJAX,代表 Asynchronous Javascript And Xml。从技术上讲,它是指浏览器(任何使用XmlHttpRequest)代表当前页面上运行的某个脚本发出的任何异步请求,无论返回什么内容类型。它还可用于描述构建页面/站点的特定模式,其中大部分/所有内容在页面上动态获取/更新。当用于描述数据格式时,“ajax”通常表示“xml”。

JSON is a data encoding format. The name itself is an acronym for "JavaScript Object Notation". JSON-formatted data looks like:

JSON 是一种数据编码格式。该名称本身是“JavaScript Object Notation”的首字母缩写词。JSON 格式的数据如下所示:

{"key": "value1", "key2": {"number": 1, "array": [0, 1, 2]}}

JSON data may be fetched by an AJAX request, though it is quite commonly used in other contexts as a lightweight, extensible, and easy-to-parse data exchange format.

JSON 数据可以通过 AJAX 请求获取,尽管它作为一种轻量级、可扩展且易于解析的数据交换格式在其他上下文中非常常用。

JSONP is simply JSON-formatted data wrapped in a callback function. The "P" stands for "with Padding", which is kind of stupid unless you like to think of function calls as "padding". In any case, JSONP data will look like:

JSONP 只是封装在回调函数中的 JSON 格式数据。“P”代表“with Padding”,这有点愚蠢,除非您喜欢将函数调用视为“padding”。无论如何,JSONP 数据将如下所示:

someFunction({"key": "value1", "key2": {"number": 1, "array": [0, 1, 2]}});

As such, JSONP is really just a JavaScript snippet, and unlike JSON is not used outside of the context of JavaScript, browsers (or other JavaScript-capable clients), and AJAX requests. The reason for using JSONP is that it allows the same-origin policyto be subverted. A script that was sourced in from site X cannot make a direct request to site Y if site Y is on a different domain from site X. But if site Y's server can send JSONP-formatted responses, then the script from site X can add a new <script>tag to the document that references a URL on site Y, and when the response from site Y is loaded it will invoke some callback function that script X has defined in the document, thus giving script X access to data that was loaded dynamically from site Y.

因此,JSONP 实际上只是一个 JavaScript 片段,与 JSON 不同的是,它不会在 JavaScript、浏览器(或其他支持 JavaScript 的客户端)和 AJAX 请求的上下文之外使用。使用 JSONP 的原因是它允许颠覆同源策略。如果站点 Y 与站点 X 位于不同的域中,则源自站点 X 的脚本无法直接请求站点 Y。但是如果站点 Y 的服务器可以发送 JSONP 格式的响应,那么站点 X 的脚本可以添加一个<script>为引用站点 Y 上的 URL 的文档添加新标记,当加载来自站点 Y 的响应时,它将调用脚本 X 在文档中定义的一些回调函数,从而使脚本 X 能够访问从站点动态加载的数据Y。

Note that JSONP data is not (typically) requested using an XmlHttpRequest. It can be done this way, subject to the standard caveats of the same-origin policy, but then you lose the cross-domain magic that makes JSONP useful in the first place.

请注意,JSONP 数据(通常)不是使用XmlHttpRequest. 可以通过这种方式完成,但要遵守同源策略的标准警告,但是这样一来,您就失去了使 JSONP 有用的跨域魔法。

REST is simply the formal spec/description of how HTTP actually works/is intended to be used. If you understand the concept of a URL being used to request a corresponding resource from a server and the difference between Getand Postthen you really know all you need to about REST.

REST 只是 HTTP 实际工作方式/打算使用方式的正式规范/描述。如果您了解用于从服务器请求相应资源的 URL 的概念以及两者之间的区别GetPost那么您就真正了解了有关 REST 的所有知识。

回答by theprogrammer

Ajax stand for Asynchronous JavaScript and Xml/XhttpRequet (the X depends and changed since mostly json is used today.

Ajax 代表异步 JavaScript 和 Xml/XhttpRequet(X 取决于并更改,因为今天主要使用 json。

It's a way to execute a request from the page using javascript to the server and receive some response. This response can be anything, json, xml, text, html, etc...

这是一种使用javascript从页面执行请求到服务器并接收一些响应的方法。此响应可以是任何内容,json、xml、text、html 等...

This makes pages look very responsive without having to reload the complete page to execute actions on it. For example posting this answer to your questions. :-)

这使得页面看起来响应非常快,而无需重新加载整个页面来对其执行操作。例如,将此答案发布到您的问题中。:-)

Json is a data format stands for JavaScrip Object Notation. It's a lighter serialization format than xml and has the advantage to be JavaScript.

Json 是一种数据格式,代表 JavaScrip Object Notation。它是一种比 xml 更轻的序列化格式,并且具有作为 JavaScript 的优势。

JsonP is the next and logical step to using Ajax with Json.

JsonP 是将 Ajax 与 Json 结合使用的下一个合乎逻辑的步骤。

A server will response with JSONP wrapping the Json object in a callback function. The name of the function is passed by the client to the server, usually as a parameter in the querystring. The P stands for padding, since the server surrounds the json object with the name of the function and the object as an argument.

服务器将使用 JSONP 将 Json 对象包装在回调函数中进行响应。函数的名称由客户端传递给服务器,通常作为查询字符串中的参数。P 代表填充,因为服务器用函数名和对象作为参数包围 json 对象。

callback({"name":"my name"});

See: http://en.wikipedia.org/wiki/JSONPfor a more detailed explanation.

有关更详细的说明,请参阅:http: //en.wikipedia.org/wiki/JSONP