Hello I am completely confused between json and ajax. When would what be used. I work with PHP on the server side. I regularly use ajax to receive data asynchronously, without invoking a page load. I use php's json functions to pass data to javascript. But I just started learning jQuery, and I am completely confused when to use function ajax and when to use json. Can somebody help me out with this, thanks.
Ajax is a concept of using JavaScript for asynchronous calls to transfer data back and forth between the client and server. It does not define what data is transferred and how it is encoded.
That's where JSON comes, JSON is a tool used for describing values and objects by encode the content in a very specific manner.
JSON (JavaScript Object Notation) and AJAX (Asynchronous JavaScript and XML) are two completely different concepts, one is used as a storage medium for data (JSON) while the other is used to retrieve data from a HTTP or FTP web server (AJAX) which is not dependent on the format of the data to be transferred, it can be text, binary, XML or JSON, which is pretty much everything.
You can use AJAX with JSON, by retrieving some data in the JSON format from the web server, using AJAX, then using JavaScript to parse the JSON data into a form accessible by the scripting engine.
I assume you are referring to the jQuery .getJSON()
method.
This method is simply a shorthand way of using the jQuery .ajax()
method with JSON data. .getJSON()
is equivalent to:
$.ajax({
url: url,
dataType: 'json',
data: data,
success: callback
});
So, you can make use of .getJSON()
if you will be retrieving JSON data from the server. The data will automatically be parsed with the jQuery.parseJSON()
method.
In more general terms AJAX is a way to asynchronously retrieve information from a server. JSON is a way to format data.... but I assume you knew this already, and you were asking specifically about the two jQuery methods I mentioned.
AJAX is used to retrieve data from a server, which can be in JSON, xML, or other formats.
AJAX is the method of access, akin to a simple web request. AJAX is asynchronous, however.
JSON is the encoding of the data returned by the access...Its on the same level of HTML or XML.
An AJAX request could return any formatting of data, and depending on the task at hand JSON or HTML may be the correct data format.
For instance, your application could request a whole new html section and use it to replace something that exists on your page already. jQuery.load()
for this. Maybe your application simply needs some data from the server, you may use jQuery.getJSON()
for this and encode your data in JSON. The nice thing about JSON, parsing it takes virtually no effort, esp with the help of jQuery.
You can think of AJAX
(Asynchronous JavaScript and XML) as a very fast postal service (this is a little bit of a stretch, AJAX
is meant to be on-demand, bear with me though). It sends information, but doesn't really know what exactly it's sending.
You can think of JSON
(JavaScript Object Notation) as a letter. It contains user-supplied and defined information in a common format which the server and client languages can understand (typically the server in ASP
, PHP
, Perl
, or Ruby
, the client browser in JavaScript
).
Just as with packages being mailed, however, the letter is not the only kind of thing that can be sent. Boxes, tubes, and other formats can be used in postage. This maps back to JSON
being one of many information formats. XML
, HTML
, Plain Text
, Binary (Images Etc)
, and any proprietary format you can think of could be sent by AJAX
to the server and back.
These crude metaphors describe the purpose of each. For technical details you will want to actually investigate implementations. MooTools
has a wrapper that allows easy use of AJAX
message passing, most other JavaScript
frameworks do as well. JQuery
is another (more popular) framework which also provides similar features.
JSON
itself is useful in that it maps directly to the object notation of JavaScript, so it is particularly convenient for storing and passing information around in. Other languages have parsers to extract and compose data in this format.
JSON is one thing only: it is a format for exchanging data. When you encode your data in JSON format, it can easily be decoded. Say you want to encode your personal information:
my name is Juan and I'm 31 years old
you would encode it in JSON format as:
{'name':'Juan', 'age':31}
that string is small, easily transmitted, and easily interpreted; it is also standardized, so anybody with a compliant JSON decoder would be able to get your name
and age
from that string. JSON is used by many web services to exchange data.
Now AJAX is not a language, or a product, or a software, or a specification, or a standard. AJAX is simply a way to do things. There is no definition of AJAX. Instead, AJAX is a collection of techniques where a given view of an Internet resource (say, a particular Web Page) can interact with the server asynchronously. For example, I am typing this message in a Text Box, and then I will hit POST YOUR ANSWER
. The server will receive this text, and put it in the site, but it will not reload the page (only the relevant part where it actually puts my answer).
AJAX is important because it provides a look-and-feel similar to a desktop application (it would be weird if every time you clicked something in your desktop application the application would close and then open again, right?)
AJAX stands for Asynchronous Javascript and XML because those two things are used, but this may change if other client-side scripting languages are developed.
So, JSON is a well specified thing (a data format) and AJAX is not (it is a way to do things, but there is no clear definition). They are unrelated, but both of them are widely used in Internet services, so you see their name pop up closely very often.