Should I use JSON or AJAX for response data?

ajax, javascript, jquery, json, xml

Solution

You are perhaps a little confused.

JSON and AJAX are not an either-or choice.

JSON and XML is a choice.

JSON and AJAX are distinct and largely unrelated, although AJAX often uses JSON, it can just easily use XML, HTML or plain text.

Or are you referring to the X in AJAX (XML)? If so, the arguments for JSON are basically:

- JSON has a smaller payload than equivalent XML; and

- JSON is easier to deal with in Javascript (compare eval'ing a JSON object to walking an XML fragment).

Other than that, it's largely personal preference.

Problem

Why JSON? I have done some tests today, and the request time for both JSON, or a normal AJAX request was the same. In the "normal request" I have returned the complete text+html tags, in the JSON request, logically I returned a "json return type" and I have created the HTML with client-side JavaScript. I don't get it, why are the big sites (Google Reader etc), or even small sites using JSON? Or do I not understand when I should use JSON?

Original source

Related problems