<script type="text/javascript" src="../js/jquery-1.9.1.min.js" >《script》 <script type="text/javascript" src="../js/cookie.js" >《script》 <script type="text/javascript" src="../js/jsorder.1.1.js" >《script》 JSORDER 案例
案例一:我的菜单(点击菜名即可加入菜单) |
红烧豆腐 12元 | 毛血旺 32元 | 套餐:京酱肉丝+2米饭 18元 |
<script type="text/javascript">//jsorder配置 $.fn.jsorder.defaults = { staticname: 'jsorder', jsorderclass: 'jsorder', savecookie: true, cookiename: 'jsorder', numpre: 'no_', jsorderpre: 'jsorder_', jsorderspanpre: 'jsorderspan_', pricefiled: 'price', namefiled: 'jsordername', leftdemo: '我的菜单', subbuttom: '', //addbuttom : 'a.jsorderadd', addbuttom: 'td.jsorderadd', nomessage: '你今天的食谱是还空的', dosubmit: function (data) { alert(JSON.stringify(data)); //$("#result").html("json内容:" + JSON.stringify(data)).css('background', '#e0e0e0'); jsonAjax("ShoppingCar.ashx", JSON.stringify(data), "text", getsuccess); } };$("body").jsorder();function jsonAjax(url, param, datat, callback) { $.ajax({ type: "post", url: url, data: param, dataType: datat, success: callback, error: function () { jQuery.fn.mBox({ message: '恢复失败' }); } });};function getsuccess(o) { //alert(o); //成功后操作} 《script》
<%@ WebHandler Language="C#" Class="ShoppingCar" %>using System;using System.Web;using System.Data;using System.Web.Script.Serialization;using System.Collections.Generic;using System.Collections;using System.IO;public class ShoppingCar : IHttpHandler{ public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; StreamReader reader = new StreamReader(context.Request.InputStream); string jsonString = HttpUtility.UrlDecode(reader.ReadToEnd()); if (MSCL.Until.IsNullOrDBNull(jsonString)) { context.Response.Write("error"); } else { jsonString = "{"goods": [" + jsonString + "]}"; DataSet ds = JsonToDataSet(jsonString); //获取的购物车商品列表 context.Response.Write("ok"); } context.Response.End(); } #region 解析Json成DataTable /// /// 解析Json成DataTable /// ///
/// public static DataSet JsonToDataSet(string Json) { try { DataSet ds = new DataSet(); DataTable dt = new DataTable("shoppingcar");
Serializer(); object obj = JSS.DeserializeObject(Json); Dictionary datajson = (Dictionary)obj; foreach (var item in datajson) { object[] rows = (object[])item.Value; foreach (var row in rows) { Dictionary valneed = (Dictionary)row; foreach (var needrow in valneed.Values) { #region Dictionary val = (Dictionary)needrow; DataRow dr = dt.NewRow(); foreach (KeyValuePair sss in val) { if (!dt.Columns.Contains(sss.Key)) { dt.Columns.Add(sss.Key.ToString()); dr[sss.Key] = sss.Value; } else dr[sss.Key] = sss.Value; } dt.Rows.Add(dr); #endregion } } } ds.Tables.Add(dt); return ds; } catch { return null; } } #endregion public bool IsReusable { get { return false; } }}