Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between json and ajax? when should what be used?

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.

like image 416
taktak Avatar asked Sep 30 '10 23:09

taktak


People also ask

Which is better AJAX or JSON?

JSON is not using for only designing the web page. In fact, JSON sometimes not at all using for the web application. AJAX is using for designing the web page properly, especially where the page needs some server-side data without refreshing the same.

When Should AJAX Be Used?

Where Should Ajax be Used? Ajax should be used anywhere in a web application where small amounts of information could be saved or retrieved from the server without posting back the entire pages. A good example of this is data validation on save actions.

Why is JSON preferred for AJAX?

Although X in Ajax stands for XML, JSON is preferred because it is lighter in size and is written in JavaScript. Both JSON and XML are used for packaging information in the Ajax model.

Should I learn JSON or AJAX first?

Javascript is the language itself so you should learn it first. JSON is the javascript object notation so while you're learning Javascript you will be learning it too. Ajax is the API for async calls to the server so it has its own object which is consumed using JS.


2 Answers

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.

like image 66
Randy the Dev Avatar answered Sep 20 '22 13:09

Randy the Dev


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.

like image 23
Peter Ajtai Avatar answered Sep 17 '22 13:09

Peter Ajtai