jpganz18 jpganz18 - 14 days ago 7
Ajax Question

What is the difference between JSON and AJAX with jQuery?

I've heard that JSON serializes all the data, which stops me having problems client side in terms of cross-browser support etc..

I've been using AJAX with jQuery and it seems easy, but I'm unsure of the differences,

I've read I can also use this to get the data:

$.ajax({
url: url,
dataType: 'json',
data: data,
success: callback
});


Can anyone explain the difference between making a jQuery AJAX request using JSON and making a jQuery AJAX request without the json type?

Will the answer be ok for all browsers?

Answer

I think you are confusing the terms.

AJAX stands for Asynchronous Javascript and XML, which is a mechanism used to launch asynchronous HTTP requests to a server using JavaScript. Don't let the name fool you; there's no restriction on you only retrieving JavaScript or XML from this technique. You can quite happily return other data formats as well (HTML, plain text and JSON, to list a few).

JSON is just one of these formats. It's a data interchange format, where-as AJAX is a technique to communicate with a server after the initate page load has completed.

To answer your question on whether you need to specify the dataType; jQuery will best guess the response format (be it HTML or JSON etc), so you're usually fine to omit it.