简体   繁体   English

在AJAX调用中返回JSON和HTML

[英]Return both JSON and HTML in AJAX call

I have a few pages with an heavy javascript usage (eg sorting and filtering of a dataset). 我有一些页面使用了大量的javascript(例如,对数据集进行排序和过滤)。

The typical usage is to display a list of complex items (usually rendered as <li> with some HTML inside): the user can delete, edit or add items with specific forms. 典型的用法是显示复杂项目列表(通常呈现为<li> ,其中包含一些HTML):用户可以删除,编辑或添加具有特定表单的项目。 Since the items are complex, I keep an array of javascript objects to perform any sort of operations, such as validate the user input before performing any action. 由于项目很复杂,我保留一组javascript对象来执行任何类型的操作,例如在执行任何操作之前验证用户输入。

The user's action and details are sent to the server via asynchronous calls: after the outcome arrives I have to update both the HTML and the javascript array. 用户的操作和详细信息通过异步调用发送到服务器:结果到达后,我必须更新HTML和javascript数组。

I use this hack: the server returns the json-encoded data structure and the updated HTML as a single string. 我使用这个hack:服务器将json编码的数据结构和更新的HTML作为单个字符串返回。 Upon data arrival, some javascript code splits the response and parses the first chunk as json (updating the array) and puts the second chunk in the inner html of the container, replacing the previous content. 在数据到达时,一些javascript代码拆分响应并将第一个块解析为json(更新数组)并将第二个块放入容器的内部html中,替换先前的内容。

I don't want to generate the HTML from the data structure, since it is not a one-man application, and the web designers change the HTML layout quite often (and independently). 我不想从数据结构生成HTML,因为它不是单人应用程序,并且Web设计人员经常(并且独立地)更改HTML布局。 Neither I want to recreate the data structure from the HTML (too complex and error-prone). 我都不想从HTML重新创建数据结构(过于复杂且容易出错)。

This system works quite nicely, having some problems only with big content, is cross-browser (is built on jQuery) and doesn't seem to have big performance issues. 这个系统运行得很好,只有大内容有一些问题,是跨浏览器(基于jQuery构建)并且似乎没有大的性能问题。

The question is: am I missing something subtle (or maybe obvious) that makes this solution bad? 问题是:我是否遗漏了使这种解决方案变得糟糕的微妙(或可能是显而易见的)的东西? Does it exists a simpler and better solution around? 它是否存在更简单,更好的解决方案?

By the way, the server runs PHP. 顺便说一句,服务器运行PHP。

Thank you. 谢谢。

Why don't you return the HTML string as part of the JSON? 为什么不将HTML字符串作为JSON的一部分返回? This way, your output is cleaner. 这样,您的输出更清晰。

So presumably you have two variables: a HTML string ( $html_string ) and an PHP array of information which will be sent in JSON format ( $array_of_info ). 所以大概有两个变量:一个HTML字符串( $html_string )和一个将以JSON格式发送的PHP信息数组( $array_of_info )。 Then: 然后:

$a = array(
  'html' => $html_string,
  'json' => $array_of_info
);
header('Content-Type: application/json');
echo json_encode($a);

The output from this is slightly longer than that from simply concatenating the HTML and JSON strings, but the parsing on the client side should be easier. 这个输出略长于简单地连接HTML和JSON字符串,但客户端的解析应该更容易。 And probably faster. 而且可能更快。 And, as pointed out in the comments, less error-prone. 而且,正如评论中指出的那样,容易出错。

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

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