M Sheik 的个人资料udooz!照片日志列表 工具 帮助

日志


5月21日

JavaScript – The Platform for Web 2.0 – Part II

The next feature in JavaScript nowadays acts a protocol between web client and server. We know that there are two types of web development technologies available in the market.

  1. Pure server side technology. (ASP.NET, Servlet)
  2. Mixed mode technology. (PHP)

The first one raises necessary level of abstraction between client and server side processing. Second one mixed with actual HTML page. Both have pros and cons. Most consumers wants the first one because of its abstraction and separation between UI rendering and UI processing. And it allows us to do object-design. Also, it provides more flexibility to interact with business services of a particular application.

How do the client and this server side talks with each other? Till pre-Ajax, we did by passing a string of HTML/CSV/Plain text/XML. The more general and gentle approach is XML. But based on customer's SLA (Service Level Agreement), the developers selected the feasible one. In any approach, we need a to and fro converter on both server and client. In server, the result of a process (generally business entities or value objects) is converted to anyone of the above approach. In client, based on the approach we need to write JavaScript code to parse these values. We cannot fully say this approach is MVC's (Model-View-Controller) baby because the OO is missing here.

Then now, is there any Ajax implementation to resolve this? Solution is available. But it is not an invention of Ajax. It is a feature of JavaScript. What is that?

In JavaScript specification, it is called as object literals.

Let us take one example. NJS Shopper is an online application for a grocery shop. After a customer placed list of items and paid online, the system finally shows him what are all items he ordered and price for these. The application server returns these information as a business entity named "OrderDetails". The figure 1 shows the details of this entity.

Figure 1

I am not going to explain the business details of the above. Before going further on how can we implement a protocol to speak both server and client in "object" context, let us see what Object Literals is?

Object Literals

We can create a JavaScript object (again object, not class) using object literals. All the members of an object can be declared as name-value pair with color separated. Each members are separated by commas and enclosed within {} as like arrays. For example,

var orderDetails = {orderID:19092, customerName:"Sheik", netAmount:2184.00};

Unlike C++/C#/Java, in JavaScript, we can create an object without any class declaration. One more proof of "Anything can be defined…" slogan.

Now think that, instead of passing HTML, XML or plain text from server to client, we can create object literals at server side and send it to client. Then, JavaScript can process the object literals. At server side, we have to write a converter for the above entity to JavaScript object literals. For example, here I've written a very basic server side code snippet for OrderDetails excluding orders field in ASP.NET 2.0.

StringBuilder orderDetailsBuilder = new StringBuilder();

 

orderDetailsBuilder.Append("{");

orderDetailsBuilder.AppendFormat("orderID:'{0}', customerName:'{1}', netAmount:{2}}}",

orderDetails.OrderID, orderDetails.CustomerName, orderDetails.NetAmount);

Response.Write(orderDetailsBuilder.ToString());

Response.End();

Caution: In .NET, using reflection we can generate object literals more general and in abstract manner. The above code snippet is just for example.

The following code snippet shows that how to handle the returned object.

var asyncReq = new ActiveXObject("Msxml2.XMLHTTP");

asyncReq.onreadystatechange = function()

{

    if (asyncReq.readyState == 4)

    {

        if (asyncReq.status == 200)

        {

          var resultString = asyncReq.responseText;

          var orderDetails = eval("(" + resultString +")");

          alert("Customer: " + orderDetails.customerName +

              " Order ID: " + orderDetails.orderID +

              " Net Amount: " + orderDetails.netAmount);

         }

    }

};

asyncReq.open("GET", "Default.aspx?a1=od", true);

asyncReq.send(null);

In AJAX period, this is called as JSON – JavaScript On Notation, a best protocol for server to client transmission.

评论 (1)

请稍候...
很抱歉,您输入的评论太长。请缩短您的评论。
您没有输入任何内容,请重试。
很抱歉,我们当前无法添加您的评论。请稍后重试。
若要添加评论,需要您的家长授予您相应权限。请求权限
您的家长禁用了评论功能。
很抱歉,我们当前无法删除您的评论。请稍后重试。
您已超过了一天之内允许提供的评论数上限。请在 24 小时后重试。
因为我们的系统表明您可能在向其他用户提供垃圾评论,您的帐户已禁用了评论功能。如果您认为我们错误地禁用了您的帐户,请联系 Windows Live 支持部门
完成下面的安全检查,您提供评论的过程才能完成。
您在安全检查中键入的字符必须与图片或音频中的字符一致。

若要添加评论,请使用您的 Windows Live ID 登录(如果您使用过 Hotmail、Messenger 或 Xbox LIVE,您就拥有 Windows Live ID)。登录


还没有 Windows Live ID 吗?请注册

没有名字发表:
http://www.nizikaikun.com/
http://www.ms-online.co.jp/eshop/ona_hole.php
http://www.ms-shop.co.jp/shop/goods/goods.asp?category=5308
http://www.ms-online.co.jp/eshop/goods/vibe.php
http://www.ms-online.co.jp/eshop/goods/costume.php
http://www.chojudo.ne.jp/
http://www.open-japan.com/
http://www.open-japan.com/ideabox/index.php?category=beauty#top
http://www.open-japan.com/ideabox/index.php?category=dress#top
http://www.eic-av.com/
http://www.nicolas-dogs.com/
http://www.aqua01.net/
http://www.kabudayo.com/
http://www.fxf-business.com/
http://kaketayo.sakura.ne.jp/
http://www.11cash.net/
http://www.saimu0.jp/
http://telink.jp/
http://www.complete-watch.com/
http://www.tokei-biho.com/
http://www.e-msqs.com/
http://www.hattori-tax.jp/
http://www.webtravel.co.jp/
http://www.webtravel.co.jp/usa/index.html
http://www.webtravel.co.jp/asia/chaina/index.html
http://www.webtravel.co.jp/aironly/index.htm
http://www.webtravel.co.jp/hotel/oahu/halekurani.htm
http://www.colorcontact1.com/
http://www.trivy-system.com/Sinyou.htm
http://www.effect-japan.com/
http://www.myhome-rd.co.jp/
http://my-trunk.jp/
http://www.kabu-net.com/
http://www.trivy-system.com/Kekkon.htm
http://www.kabarai-seikyu.com/
http://www.sigmac.jp/
http://www.e-fil.co.jp/
http://www.sample-net.com/
http://www.clasic.jp/goods/nudefitcoat.htm
http://www.clasic.jp/goods/tradcasey.htm

http://www.clasic.jp/
http://www.bit-of-sound.com/
http://www.hyousatu.com/
http://www.a-world.co.jp/
http://www.a-toy.ne.jp/
http://www.blyjapon.com/
http://adultshop.co.jp/omocha.html
http://adultshop.co.jp/adultshop.html
http://adultshop.co.jp/andropenis.html
http://adultshop.co.jp/andropenis2.html
http://adultshop.co.jp/onahole.html
http://adultshop.co.jp/houkei.html
http://adultshop.co.jp/anal.html
http://adultshop.co.jp/enemagra.html
http://adultshop.co.jp/denma.html
http://adultshop.co.jp/dutch.html
http://furniture.michiookamoto.com/
http://www.achelabo.jp/
http://www.takumi-pg.com/
http://www.omochacha.com/
http://www.av-one.jp/zero/top.html
http://umanity.jp/
http://www.bse.jp/
http://www.claudia.co.jp/tuhan/goods/beauty/yomogi_hot_sheet/index.html
http://www.claudia.co.jp/tuhan/goods/beauty/ekusute_kit/index.html
http://yumemirai.ne.jp/
http://ftb-rmt.jp/
http://ftb-rmt.jp/atlantica/
http://ftb-rmt.jp/chusen/
http://ftb-rmt.jp/arad/
http://ftb-rmt.jp/tw/
http://ftb-rmt.jp/redstone/
http://ftb-rmt.jp/nobunaga/
http://ftb-rmt.jp/ff11/
http://ftb-rmt.jp/lh/
http://ftb-rmt.jp/lineage2/
http://asaka.century21web.jp
http://niiza.century21web.jp
http://wako.century21web.jp
http://www.21tokiwa.co.jp
http://www.century21web.jp
http://shiki.century21web.jp
http://www.trivy-system.com/Sinyou.htm
http://www.trivy-system.com/Kekkon.htm
1 月 5 日

引用通告

此日志的引用通告 URL 是:
http://udooz.spaces.live.com/blog/cns!1EA86C1FB1C134B8!194.trak
引用此项的网络日志