Avovk
Avovk

Reputation: 547

Is it more efficient to let ajax get the html or get JSON and make the javascript create the html

The title says it all, but basically I use ajax to get information and I don't know if I should have PHP create the HTML which then gets returned to the client and then javascript just plugs it in or if I should have the server just send all the information through JSON and have javascript (jQuery) create all the html that holds it.

Which one is more efficient?

Thanks

Upvotes: 2

Views: 242

Answers (1)

crush
crush

Reputation: 17013

I would say it is better practice to serve only the JSON data. Why?

Well, perhaps, you want to hook up a different type of client to your data service.

Maybe you create a mobile app, and it needs the same data, but wants to display it differently.

If you are providing the HTML markup as well, then now your mobile app has to parse the data it wants out of the HTML structure, rather than just dealing with the data right away.

On an efficiency scale, that depends on what you consider efficient.

For example, it would be efficient from a bandwidth perspective to only send the JSON. However, it would be more efficient from a processing standpoint on the target client to simply give it an HTML string to display.

If you are considering ever having different clients accessing the same data, though, then you want to create a single data interface that serves JSON (in your case), and allow the client to decide how to present that data.

Separation of concerns.

Upvotes: 1

Related Questions