File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes HTML, CSS and JavaScript and the fly likes AJAX Response Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "AJAX Response" Watch "AJAX Response" New topic

AJAX Response

Paul Croarkin
Ranch Hand

Joined: Sep 30, 2004
Posts: 106
Nate and Ryan,

What are the pros and cons of the various responses to an AJAX request such as returning html, XML, or JSON?

Thanks,<br /> <br />Paul Croarkin<br />SCEA 5, SCWCD, SCJP
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63867

It really depends upon how the returned data is to be used in the page.

If you're just going to shove something into an element's innerHTML, it's easy to let a server-side termplating engine (JSP,php,Tapestry,et al) do the substitution and formatting for you and return the renderred HTML. Especially if you are using something like Prototype with its nifty Ajax.Updater facility.

If the data is going to be put into a Javascript construct, returning JSON (again, server-side templating engines are a god-send here) to be eval'd on the client is easy.

If the data is highly structured, and doesn't fit into the above categories, XML may be the best option.

Personally, I find myself using XML the least. I'm usually sending either plain text, JSP-generated HTML, or JSON. XML is used only when none of those are appropriate.

[Asking smart questions] [About Bear] [Books by Bear]
dinesh pande
Ranch Hand

Joined: Apr 07, 2003
Posts: 124
[edit: thread hi-jack removed]
[ September 20, 2006: Message edited by: Bear Bibeault ]
Eric Pascarello

Joined: Nov 08, 2001
Posts: 15385

Do not hijack another thread that has nothing to do with your question. Start your own thread and someone will give you an answer.

Nate Schutta
Ranch Hand

Joined: Mar 09, 2004
Posts: 39
You know, I've really got to second Bear's comment on using XML - I know the (former) acronym ends in X for XML but frankly I think JSON or HTML is (typically) a better approach.

Nate Schutta<br />Coauthor of <a href="" target="_blank" rel="nofollow">Foundations of Ajax</a><br />My blog: <a href="" target="_blank" rel="nofollow">Just a Thought</a>
Susanta Chatterjee
Ranch Hand

Joined: Aug 12, 2002
Posts: 102
I will agree to Bear too. We are now using AJAX for a while and we found that XML is the last choice when nothing else can fit into.

In our application, we basically get JSP formatted HTML snippets to dynamically update client in the front end using AJAX.

- Susanta
I agree. Here's the link:
subject: AJAX Response
It's not a secret anymore!