views:

107

answers:

3

Hi,

I need to return a list of blog posts (subject, body, datecreated, commentCount)

Should I return a json response or xml?

In the calling page, I will have to inject this into the page (its an ajax call).

So I need to be able to loop through the blog posts, and create HTML with the blog post info etc. and inject into the page.

I'll be using jquery.

+5  A: 

If you're going to be using this in client side javascript, then definitely don't use xml -- json is probably the way to go.

One other option to consider is to just bring down the formatted HTML. Personally, I'd go with the json and build the HTML client side, but doing that server side would be a non-crazy solution too.

Clyde
A: 

json in combination with jTemplates.

redsquare
Care to explain the -1?
redsquare
Using a template engine in JavaScript would slow down the page. Espcially if the format is well defined and does not subject to regular change. Using templates are good but not always. And this is a JS freak who is telling this so I'm not biased towards JS, I'm just being realistic.
BYK
So you would rather send down endless repeated markup. Fair enough!
redsquare
It would not slow the page down. For example a flight results page. 100's of results but very similar markup. Much better to use json and a template. 5 times as quick.
redsquare
No, I would rahter create the HTML elements as suggested by Clyde. But I'm also open to discussion. If you can convince me that using jTemplates a better solution(which I would really love to be convinced ;)) I will both learn something new and chamge my wote to a +1 =)
BYK
Er so your -1 is based not on knowledge but just a hunch. Charming. Thats not really helping the community. You should only -1 when your rock sure it is a bad solution.
redsquare
"Much better to use json and a template. 5 times as quick." can you provide evidence? I still think using gizipped plain html and inner html would be the fastest though I would prefer the josn and manual element creation.
BYK
My -1 is based on solid experience not an egoistic hunch.
BYK
ok. What happens on your site when the user wants to sort the data. You have to trawl back to the server to redo the search, with json you can do it client side as you already have the raw data. There are multiple benefits of this solution. 5 times quicker
redsquare
Please read carefully, I recommend the JSON format but not using jTemplates.
BYK
So lets see how you would generate the markup.
redsquare
Clyde suggests generating the html server side and client side.....Please also read carefully
redsquare
Using the good old document.createElement method of course? Or if someone wants to chose the easy way, I would create a pre formatted string which will nearly be equal to your template solution but will definetly be faster.
BYK
Clyde: "Personally, I'd go with the json and build the HTML client side..."BYK: "...though I would prefer the josn and manual element creation."
BYK
Clyde "One other option to consider is to just bring down the formatted HTML."
redsquare
Are we discussing what wee have said or the methods? I have stated that I'm a json person but not a jTemplates person already?
BYK
+3  A: 

A JSON object is native Javascript, definitely recommended for client-side efficiency. Use the latest version of JQuery for secure JSON loading. Jquery is easy with $.each() to loop through JSON data and append it to page elements. Hope that helps!

Al