且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JSON与HTML Ajax响应

更新时间:2022-03-29 10:06:55

取决于.在这两种情况下,服务器都只是返回带有文本的响应.如果响应的JSON版本比HTML版本需要更多的字符,则该响应将需要更长的时间才能传输回客户端,反之亦然.

Depends. In both cases, the server is simply returning a response with text. If the JSON version of the response requires more characters than the HTML version, that response will take longer to be transmitted back to the client, and vice versa.

但是,当然还有服务器端脚本必须完成其工作.在您的情况下,从服务器端脚本生成JSON可能比HTML更快.我没办法知道.

But of course there is also the server-side script which must do its work. Perhaps in your case generating JSON is faster than HTML from your server-side script. No way for me to know.

然后是客户端处理.您必须解析响应才能将其转换为真实的对象,然后您需要遍历结果对象以生成HTML.这肯定比将HTML响应并将其注入DOM所需的时间更长.

And then there is the client-side processing. You'd have to parse the response to turn it into a true object, and then you'd need to iterate over the resulting object in order to generate the HTML. This will definitely take longer than just taking an HTML response and injecting it into the DOM.

但是,我怀疑性能差异是否会很明显,这意味着您决定提供JSON响应还是HTML响应应该基于其他因素.

However, I doubt that the performance difference will be noticeable, meaning that your decision about providing a JSON response vs. HTML response should be based on other factors.