HTML 作为 AJAX 调用的结果(优点和缺点)

发布于 2024-11-09 09:05:14 字数 177 浏览 0 评论 0原文

对于返回 HTML 代码作为 AJAX 调用的结果,您有何看法(赞成和反对)。是的,如果应用程序在列表中创建一个新项目,并且需要一些额外的参数或一些模式自定义,我们可以通过 AJAX 调用将其模板化,而不是通过 JS 修改它。

要点是 HTML 片段从服务器发送到客户端计算机并集成到文档 DOM 中。这种方法有什么问题吗?

What is your opinion (PRO an CONS) about returning HTML code as a result for an AJAX call. It is, if the app creates a new item in a list and it needs some extra parameters or some pattern customization, instead of modify it through JS, we can send it templatized through an AJAX call.

The point is that HTML snippets are sent from the server to the client computer and integrated in the document DOM. Any problem with this approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

等风也等你 2024-11-16 09:05:14

完全没有问题,完全正常且合理的事情。

有时存在发送数据而不是标记并使用客户端模板扩展数据的用例,但这主要适用于发送大量数据并希望保持大小的情况在线下。 (例如,一个大表,其 HTML 表示为 100k,但 JSON 格式的原始数据仅为 10k。)或者当模板根据客户端条件而变化时。但总的来说,发送 HTML 是完全可以的,然后通过 innerHTML(或者任何几个库的包装器来帮助您解决奇怪的问题)将其合并到 DOM 中。

No problem with it at all, perfectly normal and reasonable thing to do.

There is sometimes a use-case for sending data rather than markup and expanding it with client-side templating, but that's mostly for situations where you're sending a lot of data and so want to keep the size on the wire down. (E.g., a large table where the HTML representation of it is 100k but the raw data in, say, JSON format would only be 10k.) Or when the templating varies depending on client-side conditions. But by and large, perfectly fine to send HTML you then incorporate into the DOM via innerHTML (or any of several libraries' wrappers for it that help you with the odd niggle).

﹏雨一样淡蓝的深情 2024-11-16 09:05:14

这是一种常见的方法。

如果您要将项目添加到列表中或将 Pod 的内容替换为完全不同的内容,这很好。

这也使得将 AJAX 应用于现有站点(例如覆盖或其他内容)变得更加容易,因为您可以向现有页面发出请求,然后删除不需要的部分。

然而,如果更新时只有一个值发生变化,那么您可能应该在那里使用 Json 。

This is a common approach.

If you're adding items to a list or replacing the contents of a pod with something completely different this is fine.

This also makes it easier to apply AJAX to existing sites (for example overlays or something) because you can make requests to existing pages and then strip out the bits you don't want.

However, it would be better for updates where only a value is changing then you should perhaps use Json there.

对你再特殊 2024-11-16 09:05:14

就我个人而言,我几乎总是选择接收不应用标记或格式的 JSON 响应,但这只是因为我喜欢拥有非常灵活、精细的响应,这样我就可以对返回的数据执行任何我想要的操作,而不必将其删除HTML 的。在很多情况下,这不一定是最简单或最优雅的解决方案! :)

Personally, I almost always choose to receive a JSON response with no markup or formatting applied, but that's just because I like having a really flexible, granular response so I can do whatever I want with the returned data, without having to possibly strip it out of HTML. This is NOT necessarily the easiest or most elegant solution in a lot of cases! :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文