knock.js的subscribe使用 通过$.ajax(); $get();解析JSON数据
2015-12-16 10:56
771 查看
//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 });
4000
相关文章推荐
- javascript DOM常用API总结
- Jsoup解析HTML实例及文档方法详解
- video JS实现多视频循环播放
- [译]JavaScript中的属性:定义和赋值的区别
- 一些平时工作中js的收集
- [Servlet&JSP] 监听器的使用
- AJAX 跨域请求 - JSONP获取JSON数据
- 【Javascript】计时器setInterval()
- javascript中使用JSON
- JS之typeof
- 提高代码性能,写出高质量js/jq代码常记
- 基于JavaScript获取鼠标位置的各种方法
- javaScript将json字符串转对象
- 【Javascript】window对象
- JS 跳转到指定链接
- Ext.js5引用和引用持有者(1)
- JS倒计时二
- javascript-实现日期大写
- JS之单例模块模式
- javascript基于prototype实现类似OOP继承的方法