Do you Ajax JSON or HTML?
What is the preferred way to do AJAX.
If it was for a search page written in PHP using Jquery for the AJAX
How would you handle the response
a) Have the response contain all the relevant html/styling
or
b) Send pure JSON and have a javascript function build the html/styling around the javascript variables.
I can see advantages to both. 'a' is obviously easier whilst 'b' is more efficient (although gzip would probably make the difference negligible).
I would say 'a' is the option you mostly want to stick with.
It's much easier to keep consistent templates server side (you can reuse them in non AJAX scenarios).
Client-side Templating isn't super elegant (if you're going to do it i suggest this: http://ejohn.org/blog/javascript-micro-templating/)
In terms of the styling I would include a minified stylesheet when the page loads not when loading the content.
I would go with the html:
Personally, I mostly return snippets of HTML from ajax calls, unless I need to do something with the returned data programmatically. For example:
That being said, obviously I feel that both options are valid. I'm sure purists will disagree, but sometimes returning plain ol' HTML is good enough.
链接地址: http://www.djcxy.com/p/47886.html上一篇: 什么更快? Ajax加载JSON或Ajax加载完成输出
下一篇: 你是Ajax JSON还是HTML?