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

来源:互联网 发布:js null undefined 编辑:程序博客网 时间:2024/06/06 04:01
//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    });

0 0
原创粉丝点击