简体   繁体   English

在页面加载时,数据应以JSON格式发送还是以PHP格式发送

[英]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. 我想知道让服务器ON页面加载将数据以JSON格式发送给用户并将Javascript转换为漂亮的数据是否会减少服务器的工作量,并提高效率。

Lets say when a user goes to my index page it loads a table by having the php file pull some data. 可以说,当用户进入我的索引页面时,它通过让php文件提取一些数据来加载表。 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? 如果用户启用了JavaScript,我是否应该发送带有表信息的JSON文件,然后让包含的Javascript文件将其转换为表? 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. 那就让我的PHP文件更有效地提取数据,然后用divs格式化它,然后将数据发送回去。

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. 我想最终创建一个可以说的表格,当您单击一个按钮时,它将进行Ajax调用,以获取一个包含更多数据的JSON文件,它将追加到表末尾。 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. 但是我的问题是我是否还应该花时间构建一个JavaScript文件,该文件以JSON格式获取页面加载时的初始数据并对其进行格式化。 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. 由于无论即时通讯什么都必须使一些javascript函数在初始页面加载后通过Ajax调用格式化JSON数据,我只想知道是否也应该在页面加载时获取数据并使用javascript格式化而不是使用PHP做吧。

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. 虽然一件事当然是PHP,但如果用户未启用Javascript,它将以所有样式返回它(div标签等)。 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? 但是我只是想知道的是,这是最好的方法,即使我在页面加载时也以JSON格式将所有数据发送给用户,并且让用户的javascript负责格式化吗?

If the data can change dynamically after the page loads, I would recommend generating the table in javascript, not PHP. 如果页面加载后数据可以动态更改,建议您使用javascript而不是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. 为了回答您的问题,使用javascript生成表将减轻服务器的负担,因为javascript将在客户端处理,而PHP在服务器端处理。 If the table is simple, I doubt that generating it in PHP will cause much of an impact performance-wise. 如果该表很简单,我怀疑用PHP生成它会在很大程度上影响性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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