简体   繁体   中英

On page load should the data be sent as JSON or have the PHP format it

I was wondering would it be less server intensive and more efficient to have my server ON page load send the data to the user in a JSON format and have there Javascript convert it into nice data.

Lets say when a user goes to my index page it loads a table by having the php file pull some data. Should I if the user has javascript enabled just send a JSON file with the table information and then have an included Javascript file convert it into a table? Would that be more efficient then having my php file pull the data then format it with the divs so forth and send the data back.

I want to eventually create lets say the table when you click a button will do an Ajax call to get a JSON file filled with more data that it will APPEND to the end of the table. But my question is should I also spend the time building a javascript file that takes the initial data on page load in a JSON format and formats it. I just want whatever will be most efficient. Since no matter what im going to have to make some javascript function to format JSON data from an Ajax call AFTER initial page load I just want to know if I should also have it get data on page load and have javascript format it instead of having PHP do it.

Though the one thing of course is PHP will return it with all the styling (div tags so forth) if the user doesnt have Javascript enabled. But what im just wondering is, is this the best method to do where I just send all data to the user in JSON format even on page load and have the users javascript take care of formatting it?

If the data can change dynamically after the page loads, I would recommend generating the table in javascript, not PHP. To answer your question, generating the table in javascript will take load off of the server, as javascript will be processed client-side and PHP is processed server-side. If the table is simple, I doubt that generating it in PHP will cause much of an impact performance-wise.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM