Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

AJAX Response

 
Paul Croarkin
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nate and Ryan,

What are the pros and cons of the various responses to an AJAX request such as returning html, XML, or JSON?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64972
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
dinesh pande
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[edit: thread hi-jack removed]
[ September 20, 2006: Message edited by: Bear Bibeault ]
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Susanta Chatterjee
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic