何时将 Ajax 与 Json 用于 Javascript 事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/939211/
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
When to use Ajax vs Json for Javascript events?
提问by carson welsh
I'm not so familiar with Ajax but I keep hearing Json mentioned as some kind of competing option. Is there a rule for when to use one versus the other?
我对 Ajax 不太熟悉,但我一直听到有人提到 Json 作为某种竞争选项。是否有关于何时使用一种与另一种的规则?
I'd like to process Javascript events in my app and update the front-end dynamically from the app but I'm not sure whether I need to use Ajax or Json for this.
我想在我的应用程序中处理 Javascript 事件并从应用程序动态更新前端,但我不确定是否需要为此使用 Ajax 或 Json。
采纳答案by Ryan Florence
Here's some JSON:
这是一些 JSON:
{ "taco" : "awesome", "burrito": "less awesome", "fishTaco": "1000" }
Looks pretty much like an array in this case.
在这种情况下看起来很像一个数组。
And you can use AJAX to get the JSON. I use it to return table data often. You've probably already used JSON-like data in your javascript but didn't realize it.
您可以使用 AJAX 来获取 JSON。我经常用它来返回表数据。您可能已经在 javascript 中使用过类似 JSON 的数据,但没有意识到这一点。
I prefer to request JSON data rather than XML or HTML. I find it easier to deal with than XML, and more flexible than HTML.
我更喜欢请求 JSON 数据而不是 XML 或 HTML。我发现它比 XML 更容易处理,而且比 HTML 更灵活。
回答by Andrew Hare
Ajaxand JSONare very different things.
From Ajax (programming):
来自Ajax(编程):
Ajax, sometimes written as AJAX (shorthand for asynchronous JavaScript and XML), is a group of interrelated web development techniques used on the client-side to create interactive web applications or rich Internet applications. With Ajax, web applications can retrieve data from the server asynchronously in the background without interfering with the display and behavior of the existing page. The use of Ajax has led to an increase in interactive animation on web pages and better quality of Web services thanks to the asynchronous mode. Data is retrieved using the XMLHttpRequest object. Despite the name, the use of JavaScript and XML is not actually required, nor do the requests need to be asynchronous.
Ajax,有时写成 AJAX(异步 JavaScript 和 XML 的简写),是一组相互关联的 Web 开发技术,用于在客户端创建交互式 Web 应用程序或富 Internet 应用程序。使用 Ajax,Web 应用程序可以在后台从服务器异步检索数据,而不会干扰现有页面的显示和行为。由于异步模式,Ajax 的使用导致网页上交互动画的增加和 Web 服务质量的提高。使用 XMLHttpRequest 对象检索数据。尽管名称如此,但实际上并不需要使用 JavaScript 和 XML,请求也不需要是异步的。
From Introducing JSON:
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.
JSON(JavaScript Object Notation)是一种轻量级的数据交换格式。人类很容易阅读和写作。机器很容易解析和生成。它基于 JavaScript 编程语言的子集,标准 ECMA-262 第 3 版 - 1999 年 12 月。JSON 是一种文本格式,它完全独立于语言,但使用 C 系列语言(包括 C)程序员熟悉的约定、C++、C#、Java、JavaScript、Perl、Python 等等。这些特性使 JSON 成为一种理想的数据交换语言。
回答by annakata
JSON is not a competing technology to AJAX it's just a data format.
JSON 不是 AJAX 的竞争技术,它只是一种数据格式。
Where you might hear competition is between XML and JSON, JSON having the advantage of being typically lighter and in native javascript already, XML having the advantage of portability and toolsets.
您可能会听到 XML 和 JSON 之间的竞争,JSON 具有通常更轻且已经在本机 javascript 中的优势,而 XML 具有可移植性和工具集的优势。
回答by cgp
As some have stated, these are two different things entirely. AJAXis a method of using JavaScript to initiate HTTP requests to fetch data. That data can be in any sort of format, but most commonly XML, HTML, and yes, JSON.
正如一些人所说,这是完全不同的两件事。AJAX是一种使用 JavaScript 发起 HTTP 请求来获取数据的方法。该数据可以是任何类型的格式,但最常见的是 XML、HTML 和 JSON。
So, as already mentioned, JSONis a data format much like XML or CSV, just with a different set of formatting rules.
因此,如前所述,JSON是一种与 XML 或 CSV 非常相似的数据格式,只是具有一组不同的格式规则。
回答by Natrium
Json is nothing more (and nothing less) than a format of the data you transfer with the Ajax-way of talking to the server.
Json 只不过是(也不过是)一种您使用 Ajax 方式与服务器通信传输的数据格式。
Some transfer the data in the form of xml, other just plain text, others use Json.
有些以xml的形式传输数据,有些只是纯文本,有些则使用Json。
More info about JSON on Wikipedia: http://en.wikipedia.org/wiki/Json
维基百科上关于 JSON 的更多信息:http: //en.wikipedia.org/wiki/Json
回答by Rob
Ajax and JSON aren't the same thing. Ajax is a technique combining, among other things, lightweight asynchronous requests and typically some DOM manipulation, in order to create richer user interfaces in web applications.
Ajax 和 JSON 不是一回事。Ajax 是一种结合轻量级异步请求和通常的一些 DOM 操作的技术,以便在 Web 应用程序中创建更丰富的用户界面。
The "X" in Ajax is often taken to mean "XML", and many sites using Ajax use XML as the format of the data returned by the server in response to such requests.
Ajax 中的“X”通常被认为是“XML”的意思,许多使用 Ajax 的站点都使用 XML 作为服务器响应此类请求返回的数据格式。
JSON (http://json.org) is a lightweight data interchange format based on Javascript's object notation. As such, it's easy to parse in Javascript. It's also a lot less verbose and repetitive than XML.
JSON ( http://json.org) 是一种基于 Javascript 对象表示法的轻量级数据交换格式。因此,在 Javascript 中很容易解析。它也比 XML 少得多冗长和重复。
回答by deadkarma
I think you may be talking about AJAX vs JSONP
我想你可能在谈论 AJAX 与 JSONP
http://en.wikipedia.org/wiki/Json#JSONP
http://en.wikipedia.org/wiki/Json#JSONP
If that's the case, the one benefit of using JSONP, is that you can get around the cross-site sandboxing (although, possibly introducing new security risks)
如果是这种情况,使用 JSONP 的一个好处是您可以绕过跨站点沙箱(尽管可能会引入新的安全风险)
回答by Punit Pratap Singh
AJAX is used for get data from server, whereas JSON is just a data format which can used to send data to the server.
AJAX 用于从服务器获取数据,而 JSON 只是一种数据格式,可用于将数据发送到服务器。
回答by Brian
I thought I would provide this comparison from my class lecture
我想我会从我的课堂讲座中提供这个比较
AJAX (Asynchronous JavaScript + XML) incorporates several technologies:
- Standards-based presentation using XHTML and CSS;
- Dynamic display and interaction using the Document Object Model (DOM);
- Data interchange and manipulation using XML and XSLT;
- Asynchronous data retrieval using XMLHttpRequest;
- JavaScript binding everything together.
- 使用 XHTML 和 CSS 的基于标准的表示;
- 使用文档对象模型 (DOM) 进行动态显示和交互;
- 使用 XML 和 XSLT 进行数据交换和操作;
- 使用 XMLHttpRequest 进行异步数据检索;
- JavaScript 将所有东西绑定在一起。
JSON, short for JavaScript Object Notation is a lightweight data interchange format. Its main application is in AJAX web application programming, where it serves as an alternative to the use of the XML format for data exchange between client and server.
JSON,JavaScript Object Notation 的缩写,是一种轻量级的数据交换格式。它的主要应用是在 AJAX Web 应用程序编程中,它可以替代使用 XML 格式在客户端和服务器之间进行数据交换。
回答by Mimi Bidar
The above answers are correct, but If you are new to using web technologies, all you need to know is simply that Ajax is a subset of JavaScript. It is a technology, a means for you to send only portion of the web page data back to the server to get processed and get the response back while the user is interacting with other parts of the page. This way, the Interaction with the page is consistent and seemless from the user stand point. Now, JSON on the other hand is the how you can format the data (similar to XML) that you are sending back to the server, and getting back. and it's used primarily with Ajax. It was developed and used mainly because it is lightweight and human readable, which makes it more efficient and user friendly. Hope that helps a bit.
上面的答案是正确的,但是如果您不熟悉 Web 技术,那么您只需要知道 Ajax 是 JavaScript 的一个子集。它是一种技术,一种让您仅将网页数据的一部分发送回服务器以进行处理并在用户与页面的其他部分交互时返回响应的方法。这样,从用户的角度来看,与页面的交互是一致且无缝的。现在,另一方面,JSON 是您如何格式化要发送回服务器并返回的数据(类似于 XML)。它主要用于 Ajax。它的开发和使用主要是因为它轻量级和人类可读,这使得它更加高效和用户友好。希望那些对你有帮助。

