views:

68

answers:

1

Ever since my foray into AJAX, I've always used the "whatever works" method of manipulating AJAX data returns. I'd like to know what the definitive and modern best practice is for handling data.

Is it best practice to generate the HTML via the server script and introduce the returned data on the onComplete function? Should XML/JSON be looked at first before anything? How about manipulating the returned data? Using .live() doesn't seem like it is the most efficient way.

I've never seen a definitive answer to this question. Your expertise is much appreciated.

+1  A: 

It all depends of the case you are on about!

Say, if the returning data, is used to fill up a grid, then the "datatype" would more likely be some json. in that case the best practice is to use the elements the framework you use provide to treat that specific kind of data. for example in Ext-js:

obj = Ext.util.JSON.decode(action.response.responseText);

(action) being the json object return to the success function.

Most of the popular javascript framework have functions one want to use to deal with specific data:

.load(), .eval(),etc...

After, building your html or xml would be better left on the server side. Most of server languages have tools to parse and generate json. So the best practice is definitely to trust your framework tools, and not to reinvent the wheel on every projects.

Tom