ASP.NET 后台获取backbone提交的数据

前端代码

<script>
        // 定义studentModel
        var student = Backbone.Model.extend({
            initialize:function(){},
            url:"Handler1.ashx",
            defaults:{
                Code:"100",
                Name:"user"
            }            
        });
        // 实例化
        var stu = new student();

        // 向服务器提交数据
        stu.save(null, {
            success: function (model, response) {
                alert(response);
            }
        });
    </script>

后台代码

public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            
            // 获取前端提交的json字符串
            var strJson = new System.IO.StreamReader(context.Request.InputStream).ReadToEnd();  
          
            // 将json反序列化到Stu
            JavaScriptSerializer js = new JavaScriptSerializer();
            Stu sn = js.Deserialize<Stu>(strJson);  

            context.Response.Write(sn.Name);
        }

        public class Stu
        {
            public int ID
            {
                set;
                get;
            }

            public string Code
            {
                set;
                get;
            }

            public string Name
            {
                set;
                get;
            }
        }


相关内容推荐