tags:

views:

1204

answers:

7

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?

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.

+3  A: 

Json is nothing more (and nothing less) than a format of the data you transfer with the Ajax-way of talking to the server.

Some transfer the data in the form of xml, other just plain text, others use Json.

More info about JSON on Wikipedia: http://en.wikipedia.org/wiki/Json

Natrium
+6  A: 

JSON is not a competing technology to AJAX it's just a data format.

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.


Introducing JSON

annakata
+9  A: 

Ajax and JSON are very different things.

From Ajax (programming):

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.

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.

Andrew Hare
You beat me to it. Same links even.
Nick Retallack
+3  A: 

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.

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.

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.

Rob
+3  A: 

As some have stated, these are two different things entirely. AJAX is 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.

So, as already mentioned, JSON is a data format much like XML or CSV, just with a different set of formatting rules.

altCognito
+7  A: 

Here's some 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 in your javascript but didn't realize it.

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.

rpflo
Strange that I've got two down votes on this, what did I miss?
rpflo
I think you did not answer the question. is like saying "i like xml" and question is "when to use it".
01
Hmm ... I think you didn't read the question very well. He asked when to use JSON v. when to use AJAX. The question doesn't make sense. There was clearly some confusion about the two technologies that I aimed to clarify.
rpflo
Incorrect Json. Correct is: { "taco": "awesome", "burrito": "less awesome", "fishTaco": 1000 }All strings must be quoted, with double quotes.
PhiLho
+1  A: 

I think you may be talking about AJAX vs 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)

deadkarma