aspose file tools*
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
Author

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
Marshal

Joined: Jan 10, 2002
Posts: 61652
    
  67

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] [Bear's FrontMan] [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
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Dinesh,

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

Eric
Nate Schutta
Author
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="http://www.amazon.com/exec/obidos/redirect?link_code=ur2&camp=1789&tag=ntschuttacom-20&creative=9325&path=tg/detail/-/1590595823/qid=1127567332/sr=8-1/ref=pd_bbs_1?v=glance%26s=books%26n=507846" target="_blank" rel="nofollow">Foundations of Ajax</a><br />My blog: <a href="http://www.ntschutta.com/jat/" 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
 
 
subject: AJAX Response