`

Should I include HTML markup in my JSON response?

 
阅读更多

 

In an e-commerce site, when adding an item to a cart, I'd like to show a popup window with the options you can choose. Imagine you're ordering an iPod Shuffle and now you have to choose the color and text to engrave.

I'd like the window to be modal, so I'm using a lightbox populated by an Ajax call. Now I have two options:

Option 1: Send only the data, and generate the HTML markup using JavaScript

What's nice about this is that it trims down the Ajax request to the bear minimum and doesn't mix the data with the markup.

What's not so great about this is that now I need to use JavaScript to do my rendering, instead of having a template engine on the server-side do it. I might be able to clean up the approach a bit by using a client-side templating solution.

Option 2: Send the HTML markup

What's good about this is that I can have the same server-side templating engine I'm using for the rest of my rendering tasks (Django), do the rendering of the lightbox. JavaScript is only used to insert the HTML fragment into the page. So it clearly leaves the rendering to the rendering engine. Makes sense to me.

But I don't feel comfortable mixing data and markup in an Ajax call for some reason. I'm not sure what makes me feel uneasy about it. I mean, it's the same way every web page is served up -- data plus markup -- right?

shareimprove this question
 
    
Great question. But seems to belong to stackoverflow. –  Saeed Neamati Jul 25 '11 at 2:46
1  
@SaeedNeamati Software design questions, especially whiteboard-y conceptual questions like this one areon-topic here and off-topic on Stack Overflow. –  user8 Jul 25 '11 at 2:58 

4 Answers

up vote8down voteaccepted

JSON should just contain the data and no markup. In the long run this approach is more extensible because there is potential for using the JSON data in other parts of your site. If you include markup then using the same data to populate another template becomes much harder.

shareimprove this answer
 
2  
Great point about the reuse. Everyone seems to be voting for the Ajax request to only contain data, and no markup, for various reasons. But this one is a big one. Thanks. –  Mike M. Lin Jul 25 '11 at 3:36
2  
Not to mention that JSON without markup is smaller and uses less bandwidth. –  Jack Maney Sep 12 '12 at 22:53
    
@JackManey: Also a good point. But the true cost of additional bandwidth isn't all that much after you already eat the cost of the HTTP request. And I can imagine some setups where you know the client machines are so crappy that it would cost you more to generate the DOM real-time using JavaScript. Think: Internal apps with users on ancient machines/browsers. –  Mike M. Lin Sep 16 '12 at 5:41

I would send the data in the request and build up the markup in js. One extra benefit would be is that there would be less bandwidth usage. It's kind of a personal preference but keeping client side markup away from the server side is probably a better idea. I have a Django website as well and I only use the templating system for putting some json varibles on the page (one less ajax req. to make) and using src files when developing on my machine. All of the client side is done with ExtJS.

shareimprove this answer
 
    
I like your idea of burning the JSON data into the page -- not here since I don't fetch the additional data until I know what item you're adding to your cart. Making a request for the page layout, then a second for the data seems to be common these days. Having the data in a JavaScript variable eliminates the need for the second HTTP request without having the have two different bits of rendering code (i.e, client and server). – Mike M. Lin Jul 25 '11 at 3:33
    
Oh I see your server is not advanced enough to know what the user is going to put in the cart beforehand ;). –  pllee Jul 25 '11 at 15:40

I think you've talk about the pros and cons of both. Why not look at a 3rd option of having the javascript for your lightbox be generated from a django view. Then have your JSON just contain the data to update it for each view?

What you should be able to do is wrap all the templating code into javascript variables and then output them with javascript after it's received the JSON request on the client side.

shareimprove this answer
 
    
Thanks for the reply. That's actually what I meant in option 1. The lightbox as a container would be burned into the page but hidden. The JSON data from the Ajax request would be used to generate the content in the container. Some of that content would be marked up using HTML. How did you interpret option 1? Maybe thatcan be my third option. –  Mike M. Lin Jul 25 '11 at 3:28
    
I was thinking something along the lines of this stackoverflow.com/questions/6008908/… then using JSON to replace the data after the initial load –  Ryan Gibbons Jul 25 '11 at 4:10

You should use the template engine for markup and keep a hidden field somewhere to store values, that you can then find in the document using a selector.

分享到:
评论

相关推荐

    HTML版JsonViewer

    JSON (JavaScript Object Notation) 是一种轻量级的数据交换格式,它基于JavaScript的一个子集,易于人阅读和编写,同时也易于机器解析和生成。在Web开发中,JSON被广泛用于前后端数据传输,因为它简洁、直观且易于...

    HTML json格式化编辑器

    HTML(HyperText Markup Language)是用于创建网页的标准标记语言,而JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,通常用于服务器向客户端传输数据。在这个场景中,"HTML json格式化编辑器"是指...

    jsoneditor 后台管理展示Json数据的专用模板 (html + js +css)

    JSONEditor是一款强大的在线JSON编辑器,它以直观的树形图和代码视图形式呈现JSON数据,非常适合在后台管理系统中用于展示和编辑JSON结构。这个模板包含HTML、JavaScript(js)和CSS三个主要部分,提供了完整的前端...

    HTML5 Quick Markup Reference

    This book is a condensed reference for HTML5 markup. It presents the essential HTML5 elements and attributes in a well-organized format that can be used as a handy reference. HTML5 Quick Markup ...

    js+html+json增删改查带验证

    在IT领域,JavaScript(js)、HTML和JSON是构建交互式Web应用的核心技术。"js+html+json增删改查带验证"这个项目是关于如何使用这些技术来创建一个具有数据管理功能的前端界面,包括数据的添加、删除、修改以及用户...

    纯前端html+js实现的excel转json格式

    标题 "纯前端html+js实现的excel转json格式" 描述了一个完全在浏览器环境中完成的Excel到JSON转换功能。这个技术方案特别适用于那些不需要服务器端处理的场景,比如小型项目或者临时的数据导入需求。它允许用户上传...

    Html页面动态生成Json

    .then(response => response.json()) .then(data => console.log('Data sent:', data)) .catch(error => console.error('Error:', error)); ``` 在上述代码中,我们向服务器的`/api/submit`接口发送了一个POST请求...

    Xml_Json_Html美化格式化工具_V4.2_源码_纯净版

    Xml、Json和Html是数据交换和网页展示中常见的三种格式,它们在开发过程中扮演着重要角色。本资源提供的是一个名为"Xml_Json_Html美化格式化工具_V4.2"的源码纯净版,主要功能是将这三种格式的数据进行美化和格式化...

    xml,json,html格式化工具

    XML(eXtensible Markup Language)、JSON(JavaScript Object Notation)和HTML(Hypertext Markup Language)是三种广泛使用的数据交换和表示格式。在处理这些文件时,有时我们需要将它们格式化,以便于阅读和调试...

    Xml_Json_Html美化格式化工具_V3.0源码

    Xml_Json_Html美化格式化工具_V3.0是一款实用的源码项目,主要用于XML、JSON和HTML数据的格式化工作。这个工具是用Visual Studio 2010开发的,要求运行环境为.NET Framework 4.0。下面将详细阐述这款工具涉及的关键...

    HTML: 1.HTML是什么? 超文本 (HyperText) 标记 (Markup) 语言 (Language) 是一种用来创建网页的标准标记语言。

    超文本 (HyperText) 标记 (Markup) 语言 (Language) 是一种用来创建网页的标准标记语言。XML XHTML HTML运行在浏览器上,默认由浏览器解析。 HTML是一套标记语言,不是一套编程语言。 标记语言是一套标记标签...

    php xml 转json和array

    在IT行业中,XML(eXtensible Markup Language)和JSON(JavaScript Object Notation)是两种广泛使用的数据交换格式。XML以其结构化和可扩展性在Web服务和数据存储中占据重要地位,而JSON则因其简洁性和易读性在...

    Xml2Json and Json2Xml

    XML(Extensible Markup Language)和JSON(JavaScript Object Notation)是两种常见的数据交换格式,广泛应用于Web服务和数据传输。XML是一种结构化语言,用于描述数据,而JSON更简洁,通常用于JavaScript应用程序...

    json自动转换成xml的json包

    JSON(JavaScript Object Notation)和XML(eXtensible Markup Language)是两种广泛使用的数据交换格式,它们在互联网上用于传递和存储结构化数据。JSON以其简洁和易于阅读的特性,通常被用作轻量级的数据交换格式...

    Android XML和JSON解析代码

    XML(Extensible Markup Language)是一种结构化的数据格式,常用于存储和传输数据,而JSON(JavaScript Object Notation)则是轻量级的、易于人阅读和编写的数据交换格式。本篇将深入探讨Android平台下如何对XML和...

    xml,json,html格式化工具 源代码

    XML(eXtensible Markup Language)、JSON(JavaScript Object Notation)和HTML(HyperText Markup Language)是三种常见的数据交换和展示格式,在Web开发和数据处理中广泛应用。本资源提供了这三种格式化的源代码...

    【HTML+CSS+JS+JSON+ES6笔记】.zip

    【HTML+CSS+JS+JSON+ES6笔记】是一份综合性的学习资料,涵盖了Web开发中的基础和进阶知识。这份笔记以HTML、CSS、JavaScript、JSON和ES6为主要讲解对象,旨在帮助学习者深入理解这五种技术,并能够熟练应用在实际...

    HTML5.Quick.Markup.Reference

    This book is a condensed reference for HTML5 markup. It presents the essential HTML5 elements and attributes in a well-organized format that can be used as a handy reference. HTML5 Quick Markup ...

    json报文与XML互转

    JSON(JavaScript Object Notation)和XML(eXtensible Markup Language)是两种广泛使用的数据交换格式,它们在web服务和应用程序之间传输数据时起到关键作用。本文将深入讲解如何在Java环境中将Java对象与JSON报文...

    xml,json,html,格式化工具

    XML(Extensible Markup Language)、JSON(JavaScript Object Notation)和HTML(HyperText Markup Language)是三种常见的数据交换和展示格式,广泛应用于Web开发和数据存储。格式化工具则是用来优化这些文件的...

Global site tag (gtag.js) - Google Analytics