knock.js的subscribe使用 通过$.ajax(); $get();解析JSON数据

//CS代码
<pre class="csharp" name="code">public ActionResult GetPhysicalSKUCost(int RequestTypeID, int SystemTypeID, int PhySKUID)
        {
            string serviceAddress = ConfigurationManager.AppSettings["MyCapacityService"].ToString();
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(serviceAddress + "/json/GetPhysicalSKUCost?RequestTypeID=" + RequestTypeID + "&SystemTypeID=" + SystemTypeID + "&PhySKUID=" + PhySKUID);
            string result = string.Empty;
            StreamReader responseStream = null;
            HttpWebResponse webresponse = null;
            try
            {
                webresponse = (HttpWebResponse)request.GetResponse();
            }
            catch (WebException ex)
            {
                webresponse = (HttpWebResponse)ex.Response;
            }
            Encoding enc = System.Text.Encoding.GetEncoding("utf-8");
            responseStream = new StreamReader(webresponse.GetResponseStream(), enc);

            result = responseStream.ReadToEnd();
            responseStream.Close();
            webresponse.Close();
            return Json(new { success = true, data = result }, JsonRequestBehavior.AllowGet);
        }


//返回的JSON数据

{"Root":{"IsAddable":"0","DomainEntity":{"ID":"11000189","Name":"PHY - Standard","DynamicAttributeName":"Custom Column (Effective FY)","DynamicAttributeValue":"256.99","Code":"PSKU000008","TypeFlag":"ECS only","GroupID":"2030","UpdateBy":"REDMOND\\v-brenpe","UpdateDt":"10/01/2014","IsExtensible":"true"}}}


 
</pre><pre class="javascript" name="code">//JS代码
self.selPhySKU.subscribe(function (newValue) {

        self.BindStorage(
                self.selRequestType() ? self.selRequestType().Code() : "",
                self.selServRole() ? self.selServRole().ID() : 0,
                self.SKUID(),
                self.selClusterType() ? self.selClusterType().ID() : 0,
                self.selSystemType() ? self.selSystemType().ID() : 0,
                self.selDatacenter() ? self.selDatacenter().ID() : 0,
                self.requestID() ? self.requestID() : 0
            );
        self.ResetSQLVersion();
        if (newValue && newValue.ID() != 10150 && self.isLoading == false && self.CurrentPage == "admin") {
            self.BindDefaultSKUOrdered(newValue.ID());
        }
        self.GetStorageLimit(self.selDatacenter() ? self.selDatacenter().ID() : 0,
                             self.selVBOSlocation() ? self.selVBOSlocation().ID() : 0,
                             newValue ? newValue.ID() : 0);
        
        //add by penglu on 20151214 for bug 15725
        //RequestType="Physical";SystemType="SystemType"
        if (self.selRequestType() != null && self.selRequestType().ID() == 390 && self.selSystemType() != null && self.selSystemType().ID() == 700 && self.selPhySKU() != null)
        {
            //method 1
            if (newValue.ID() == 10150) { //PHY Non-standard/Other
                if ($("#divPhysicalSKUCost").length > 0) { //Clear divSKUCost Content
                    //$("#divPhysicalSKUCost").empty();
                    $("#divPhysicalSKUCost").remove();
                }
                //Right below the SKU dropdown Display the price 0 for the selected "PHY Non-standard/Other"
                $("#Dep_Mes_Physical").after("<div class='k-icon k-warning' id='divPhysicalSKUCost'>The selected SKU will cost $ 0<br /></div>");
            }
            else {
                method 1
                //$.ajax({
                //    type: "GET",
                //    url: "/Order/order/GetPhysicalSKUCost",
                //    data: { "RequestTypeID": self.selRequestType().ID(), "SystemTypeID": self.selSystemType().ID(), "PhySKUID": self.selPhySKU().ID() },
                //    dataType: "json",
                //    success: function (item) {
                //        var varJson = JSON.parse(item.data);
                //        var varPhysicalSKUCost = (varJson != null && varJson.Root.DomainEntity != null) ? varJson.Root.DomainEntity.DynamicAttributeValue : 0;
                //        if ($("#divPhysicalSKUCost").length > 0) { //Clear divSKUCost Content
                //            //$("#divPhysicalSKUCost").empty();
                //            $("#divPhysicalSKUCost").remove();
                //        }

                //        //Right below the SKU dropdown Display the price for the selected SKU
                //        $("#Dep_Mes_Physical").after("<div class='k-icon k-warning' id='divPhysicalSKUCost'>The selected SKU will cost $ " + varPhysicalSKUCost + "<br /></div>");

                //        //Add a pop up dialog after selecting a SKU that shows "The selected SKU will cost $ XX.XX" WITH an OK Button
                //        var popupHtml = "<div id='req-edit-Popup' class='font-S-13'>"
                //                      + "<div class='p-bottom-20'>The selected SKU will cost $ " + varPhysicalSKUCost + "<br /></div>"
                //                      + "<div><a class='btn-yes popup-close'>OK</a></div>";
                //        McUtility.showNoClosePopUpPage(popupHtml, 400);
                //    }
                //});

                //method 2
                $.get("/Order/order/GetPhysicalSKUCost",
                    { "RequestTypeID": self.selRequestType().ID(), "SystemTypeID": self.selSystemType().ID(), "PhySKUID": self.selPhySKU().ID() },
                    function (item) {
                        var varJson = JSON.parse(item.data);
                        var varPhysicalSKUCost = (varJson != null && varJson.Root.DomainEntity != null) ? varJson.Root.DomainEntity.DynamicAttributeValue : 0;
                        if ($("#divPhysicalSKUCost").length > 0) { //Clear divSKUCost Content
                            //$("#divPhysicalSKUCost").empty();
                            $("#divPhysicalSKUCost").remove();
                        }

                        //Right below the SKU dropdown Display the price for the selected SKU
                        $("#Dep_Mes_Physical").after("<div class='k-icon k-warning' id='divPhysicalSKUCost'>The selected SKU will cost $ " + varPhysicalSKUCost + "<br /></div>");

                        //Add a pop up dialog after selecting a SKU that shows "The selected SKU will cost $ XX.XX" WITH an OK Button
                        var popupHtml = "<div id='req-edit-Popup' class='font-S-13'>"
                                      + "<div class='p-bottom-20'>The selected SKU will cost $ " + varPhysicalSKUCost + "<br /></div>"
                                      + "<div><a class='btn-yes popup-close'>OK</a></div>";
                        McUtility.showNoClosePopUpPage(popupHtml, 400);
                    },
                    "json");
            }                        
        }
        //add by penglu on 20151214 for bug 15725
    });

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值