ImageVerifierCode 换一换
格式:DOCX , 页数:10 ,大小:25.85KB ,
资源ID:3517039      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/3517039.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(IT专家为什么返回生成的HTML而不是JSON是不好的做法或者是吗.docx)为本站会员(b****4)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

IT专家为什么返回生成的HTML而不是JSON是不好的做法或者是吗.docx

1、IT专家为什么返回生成的HTML而不是JSON是不好的做法或者是吗本文由我司收集整编,推荐下载,如有疑问,请与我司联系为什么返回生成的 HTML 而不是 JSON 是不好的做法?或者是吗?It is quite easy to load HTML content from your custom URLs/Web services usingJQuery or any other similar framework. Ive used this approach many times and till nowand found the performance satisfactory.使用 J

2、Query 或其他类似框架从自定义 url /Web 服务加载 HTML 内容非常容易。我已经多次使用这种方法,到目前为止,我发现性能令人满意。 But all the books, all the experts are trying to get me to use JSON instead of generatedHTML. Hows it much more superior than HTML?但是所有的书籍,所有的专家都试图让我使用 JSON 而不是生成的 HTML。它比HTML 有什么优势呢? Is it very much faster? Does it have a very

3、 much lesser load on the server?快得多吗?它在服务器上的负载要小得多吗? On the other side I have some reasons for using generated HTML.另一方面,我有一些使用生成的 HTML 的理由。 Its simple markup, and often just as compact or actually more compact than JSON.它是简单的标记,通常和 JSON 一样紧凑或者更紧凑。 Its less error prone cause allyoure getting is mark

4、up, and no code. 它不容易出错,因为您得到的只是标记,没有代码。 It will be faster to program in most cases cause you wont have to write codeseparately for the client end. 在大多数情况下,编程会更快,因为您不必为客户端单独编写代码。 Which side are you on and why?你站在哪一边,为什么? 235Im a bit on both sides, actually :事实上,我有点两面性: When what I need on the javasc

5、ript side is data, I use JSON 当我在 javascript 端需要的是数据时,我使用 JSON When what I need on the javascript side is presentation本文由我司收集整编,推荐下载,如有疑问,请与我司联系on which I will not do any calculation, I generally use HTML 当我在 javascript 方面需要的是表示时,我不会做任何计算,我通常使用 HTML The main advantage of usingHTML is when you want t

6、o replace a full portion of your page with what comes back fromthe Ajax request :使用 HTML 的主要优点是,当您希望用 Ajax 请求返回的内容替换整个页面时: Re-building a portion of page in JS is (quite) hard 用 JS 重新构建页面的一部分是非常困难的 You probably already have some templating engine on the server side, thatwas used to generate the page

7、in the first place. Why not reuse it ? 您可能已经在服务器端有了一些模板引擎,这些引擎最初用于生成页面为什么不重用它呢? Igenerally dont really take into consideration the “performance” side of things, at least onthe server :我通常不会考虑事情的“性能”方面,至少在服务器上是这样的: On the server, generating a portion of HTML or some JSON wont probably makethat much o

8、f a difference 在服务器上,生成一部分 HTML 或一些 JSON 可能不会有太大的差别 About the size of the stuff that goes through the network : well, youprobably dont use hundreds of KB of data/html. Using gzip on whatever you aretransferring is whats going to make the biggest difference (not choosing between HTMLand JSON) 关于通过网络的

9、东西的大小:嗯,你可能不会使用几百 KB 的数据/html在传输的任何东西上使用 gzip 都将产生最大的差异(而不是在 HTML 和 JSON 之间进行选择) One thing that could be taken into consideration, though, is what resourcesyoull need on the client to recreate the HTML (or the DOM structure) from the JSONdata. compare that to pushing a portion of HTML into the page

10、;-) 不过,可以考虑的一点是,客户端需要什么资源来从 JSON 数据中重新创建 HTML(或 DOM 结构)将其与将 HTML 的一部分放入页面进行比较;-) Finally, one thing that definitly matters :最后,有一点非常重要: How long will it take you to develop a new system that will send data as JSON +本文由我司收集整编,推荐下载,如有疑问,请与我司联系code the JS required to inject it as HTML into the page ? 开

11、发一个新系统需要多长时间才能将数据以 JSON +代码的形式发送到页面中? How long will it take to just returnHTML ? And how long if you can re-use some of your already existing server-side code ?返回 HTML 需要多长时间?如果您可以重用一些已经存在的服务器端代码,需要多长时间? And to answer another answer : if you need to update more than one portion ofthe page, there is

12、still the solution/hack of sending all those parts inside one big string thatgroups several HTML portions, and extract the relevant parts in JS.为了回答另一个问题:如果需要更新页面的多个部分,仍然可以将所有这些部分发送到一个大字符串中,该字符串将多个 HTML 部分分组,并用 JS 提取相关部分。 For instance, you could return some string that looks like this :例如,您可以返回如下所示的

13、字符串: !- MARKER_BEGIN_PART1 - here goes the htmlcode for part 1 !-MARKER_END_PART1 - !- MARKER_BEGIN_PART2 - here goes the htmlcodefor part 2 !- MARKER_END_PART2 - !- MARKER_BEGIN_PART3 - here goesthe json datathat will be used to build part 3from the JS code !- MARKER_END_PART3- That doesnt look rea

14、lly good, but its definitly useful (Ive used it quite a couple oftimes, mostly when the HTML data were too big to be encapsulated into JSON) : you aresending HTML for the portions of the page that need presentation, and you are sendingJSON for the situation you need data.这看起来不很好,但是在街上有用(我使用了很几次,通常是在

15、 HTML 数据太大被封装成 JSON):你发送 HTML 页面的部分需要演讲,和你发送 JSON 的情况你需要数据 . And to extract those, the JS substring method will do the trick, I suppose ;-)要提取这些,我想 JS 子字符串方法可以起到这个作用;-) 102I mainly agree with the opinions stated here. I just wanted to summarize them as:本文由我司收集整编,推荐下载,如有疑问,请与我司联系我主要同意这里的观点。我想总结一下 It

16、is bad practice to send HTML if you end up parsing it client-side to do somecalculations over it.如果您最终对 HTML 进行客户端解析并对其进行一些计算,那么发送 HTML 是不好的做法。 It is bad practice to send JSON if all youll end up doing is to incorporate it into thepages DOM tree.如果您最终要做的只是将 JSON 合并到页面的 DOM 树中,那么发送 JSON 是不好的做法。 好吧, I

17、m one of those rare persons that likes to separate things this way: - The server isresponsible for delivering data (model); - The client is responsible for showing (view) andmanipulating data (model);我是那种喜欢以这种方式分离事物的少数人之一:服务器负责交付数据(模型);-客户端负责显示(视图)和操作数据(模型); So, the server should focus on delivering

18、 the model (in this case JSON is better). Thisway you get a flexible approach. If you want to change the view of you model, you keepthe server sending the same data and just change the client, javascript components, thatchange that data into a view. Imagine, you have a server delivering data to mobi

19、le devicesas well as desktop apps.因此,服务器应该专注于交付模型(在这种情况下,JSON 更好)。这样你就有了一个灵活的方法。如果您想要更改模型的视图,您可以让服务器发送相同的数据,并仅更改客户机、javascript 组件,将数据更改为视图。想象一下,你有一个服务器将数据传送到移动设备和桌面应用程序。 Also, this approach increases productivity, since the server and client code can be builtat the same time, never losing the focus

20、which is what happens when you keep switching本文由我司收集整编,推荐下载,如有疑问,请与我司联系from js to PHP / JAVA / etc.另外,这种方法提高了生产率,因为服务器和客户端代码可以同时构建,永远不会失去焦点,这是当您从 js 切换到 PHP / JAVA /等时所发生的事情。 Generally, I think most people prefer to do as much as possible on the server sidebecause they dont master js, so they try to

21、 avoid it as much as possible.一般来说,我认为大多数人喜欢在服务器端尽可能多地做,因为他们不精通 js,因此他们尽量避免使用 js。 Basically, I have the same opinion as those guys that are working on Angular. In myopinion that is the future of web apps.基本上,我和那些研究角度的人有相同的观点。在我看来,这就是网络应用的未来。 9I have something interesting I thought I might add. I dev

22、eloped an application that onlyever loaded a full view one time. From that point forward it communicated back to theserver with ajax only. It only ever needed to load one page (my reason for this isunimportant here). The interesting part comes in that I had a special need to return somedata to be op

23、erated on in the javascript AND a partial view to be displayed. I could havesplit this up into two calls to two separate action methods but I decided to go withsomething a little more fun.我有一些有趣的东西,我想我可以添加。我开发了一个应用程序,它只加载了一次完整的视图。从此以后,它只使用 ajax 与服务器通信。它只需要加载一个页面(我的原因在这里不重要)。有趣的是,我特别需要返回一些要在 javascri

24、pt 中操作的数据和显示的部分视图。我本可以把它分成两个调用,分别调用两个不同的动作方法,但我决定用一些更有趣的东西。 Check it out:检查一下:本文由我司收集整编,推荐下载,如有疑问,请与我司联系 public JsonResult MyJsonObject(string someData) return Json(new SomeData =someData, PartialView = RenderPartialViewToString(“JsonPartialView”, null) ,JsonRequestBehavior.AllowGet); What is Render

25、PartialViewToString() you might ask? Itis this little nugget of coolness right here:您可能会问什么是 RenderPartialViewToString() ?这是一个小小的冷元素: protected string RenderPartialViewToString(string viewName, object model)ViewData.Model = model; using (StringWriter sw = new StringWriter()ViewEngineResult viewResul

26、t =ViewEngines.Engines.FindPartialView(ControllerContext, viewName); ViewContextviewContext = new ViewContext(ControllerContext, viewResult.View, ViewData,TempData, sw); viewResult.View.Render(viewContext, sw); returnsw.GetStringBuilder().ToString(); I havent done any performance testing on this so

27、Imnot sure if it incurs any more or less overhead than calling one action method for theJsonResult and one for the ParticalViewResult, but I still thought it was pretty cool. It justserializes a partial view into a string and sends it along with the Json as one of itsparameters. I then use JQuery to

28、 take that parameter and slap it into its appropriate DOMnode :)我还没有做过任何性能测试,因此我不确定它是否会比调用 JsonResult 的一个操作方法和一个粒子 viewresult 来产生更多的开销,但是我仍然认为它很酷。它只是将部分视图序列化为字符串,并将其连同 Json 作为参数之一发送出去。然后,我使用 JQuery 将该参数添加到相应的 DOM 节点中:) Let me know what you think of my hybrid!让我知道你对我的混血儿的看法! 8If the response needs no

29、 further client-side processing, HTML is OK in my opinion.Sending JSON will only force you to do that client-side processing.本文由我司收集整编,推荐下载,如有疑问,请与我司联系engines every day. So the speed of client parsing is not an important issue right now, evenmore, JSON objects are ususally very small so they dont co

30、nsume a lot of client sideresourses. I prefer to have a slow website for some users with slow browser rather thanslow site for everyone because of very loaded server.JSON 是非常多功能性和轻量级的格式。当我开始使用它作为客户端模板解析器数据时,我发现了它的美妙之处。让我解释一下,以前我在服务器端使用 smarty 和视图(生成高服务器负载),现在我使用一些自定义 jquery 函数,所有数据都在客户端呈现,使用客户端浏览器作为

31、模板解析器。它节省了服务器资源,另一方面浏览器每天都在改进它们的 JS 引擎。因此,客户端解析的速度现在并不是一个重要的问题,而且 JSON 对象通常非常小,因此它们不会消耗大量客户端资源。我更喜欢用慢的浏览器而不是慢速的网站给每个人,因为每个人都是很有负载的服务器。 On another hand, sending pure data from server you abstract it from presentation so, iftomorrow you want to change it or integrate your data into another service you can do itmuch easier.另一方面,从服务器发送纯数据,您可以从表示中抽象数据,因此,如果明天想要更改数据或将数据集成到另一个服务中,您可以更容易地完成。 Just my 2 cents.那只是我的个人意见。 6If you want a clean decoupled client, which in my opinion is best practice, then it makessense

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1