AJAX & JSON

All about creating websites!
Post Reply
User avatar
l0ngb1t
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 598
Joined: 15 Apr 2009, 16:00
15
Contact:

AJAX & JSON

Post by l0ngb1t »

Where it is better to use AJAX and where it is better to use JSON? can we say that one is better than the other? which one is less resource consuming on the server (CPU mainly)?
or both are equally good ?

i need your opinion!
Much appreciated :wink:
There is an UNEQUAL amount of good and bad in most things, the trick is to work out the ratio and act accordingly. "The Jester"

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Re: AJAX & JSON

Post by ayu »

l0ngb1t wrote:Where it is better to use AJAX and where it is better to use JSON? can we say that one is better than the other? which one is less resource consuming on the server (CPU mainly)?
or both are equally good ?

i need your opinion!
Much appreciated :wink:

AJAX and JSON are two completely different things.

Code: Select all

http://en.wikipedia.org/wiki/Ajax_%28programming%29

Code: Select all

http://en.wikipedia.org/wiki/Json
"The best place to hide a tree, is in a forest"

User avatar
l0ngb1t
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 598
Joined: 15 Apr 2009, 16:00
15
Contact:

Re: AJAX & JSON

Post by l0ngb1t »

sure, but when used (json) with jquery it could do the same job as AJAX i guess, so what is better ?
There is an UNEQUAL amount of good and bad in most things, the trick is to work out the ratio and act accordingly. "The Jester"

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Re: AJAX & JSON

Post by ayu »

l0ngb1t wrote:sure, but when used (json) with jquery it could do the same job as AJAX i guess, so what is better ?

I still do not think that you fully understand what they both mean.
No offense, and if you think I misunderstand then please explain (I am not here to offend you or point out who is right or wrong. I'm merely trying to help you :))

Anyway, let's clear up a few things first

* jquery is a JavaScript library that can help you with loads of things, from making nice effects on your website, to loading less data with the help of AJAX requests.
* AJAX is a method of asynchronously sending and receiving data with JavaScript. The most common use of it is to reduce page loads and request less data from the server.
* JSON is a structure or format of data, much like XML. Although I prefer JSON when sending data, as it uses up less bytes (I use XML for structuring config files for example, but I would never send them over the network)

Now, you can use all these together.
You could use an AJAX request with jquery and send everything using JSON, or you can use any other AJAX library to do the same and send the JSON data.

So, to summarize it, jquery is the library containing functions for sending data with the AJAX concept, and JSON is the way you structure the data being sent.

A common example would be something like the code below (Reference: intelligrape.com)

Code: Select all

var jsonObjects = [{id:1, name:"amit"}, {id:2, name:"ankit"},{id:3, name:"atin"},{id:1, name:"puneet"}];
 
jQuery.ajax({
          url: <Url of the action>,
          type: "POST",
          data: {students: JSON.stringify(jsonObjects) },
          dataType: "json",
          beforeSend: function(x) {
            if (x && x.overrideMimeType) {
              x.overrideMimeType("application/j-son;charset=UTF-8");
            }
          },
          success: function(result) {
 	     //Write your code here
          }
});
In the example above, you create an array of JSON objects (jsonObjects), and send it using one of the AJAX methods of the jquery library.

Using this method when updating website content is preferred, as you can control exactly what part of the website is being updated, instead of reloading the whole website and sending a lot more data than you actually need to.
But AJAX should also be used with care, as if you overdo it you risk sending too many requests on one page (we did this mistake in a recent project where I work), and end up stressing the server more than needed.
"The best place to hide a tree, is in a forest"

User avatar
l0ngb1t
Fame ! Where are the chicks?!
Fame ! Where are the chicks?!
Posts: 598
Joined: 15 Apr 2009, 16:00
15
Contact:

Re: AJAX & JSON

Post by l0ngb1t »

all cleared up, thank you cat...
i guess i have to make my self a fool in public from time to time in order to learn. :lol:
There is an UNEQUAL amount of good and bad in most things, the trick is to work out the ratio and act accordingly. "The Jester"

User avatar
ayu
Staff
Staff
Posts: 8109
Joined: 27 Aug 2005, 16:00
18
Contact:

Re: AJAX & JSON

Post by ayu »

l0ngb1t wrote:all cleared up, thank you cat...
i guess i have to make my self a fool in public from time to time in order to learn. :lol:
Not a fool.
There are no stupid questions.

If you ever feel like "this is a stupid question, I don't dare to ask", then ask it here on this forum.
"The best place to hide a tree, is in a forest"

Post Reply