Jquery Ajax 複雜json對象提交到WebService

一。使用get方式
1.前臺
            //複雜json對象提交
            var person = {'per':"{ 'id': 1, 'name': '張三', 'sex': '男' }"};
            $.ajax({
                type: "get",
                url: "JsonObject.asmx/GetPersonByObject",
                data: person,
                dataType: 'json',
                contentType: 'application/json;charset=utf-8',
                success: function (data) {
                    if (data.d == "1") {
                        $("#hello").text("服務器接收成功!");
                    }
                    else {
                        $("#hello").text("服務器接收數據失敗!");
                    }
                },
                error: function () {
                    $("#hello").text("程序運行出錯!");
                }
            });
2.後臺
        [WebMethod]
        [ScriptMethod(ResponseFormat=ResponseFormat.Json, UseHttpGet=true)]
        public string GetPersonByObject()
        {
            string jsonStr = HttpContext.Current.Request["per"];
            Person per = jsonStr.JsonDeserialezer<Person>();//將json字符串反序列化
            if (per.Id == 1)
            {
                return "1";
            }
            return "0";
        }
 
二。使用post方式
1.前臺
            var person = "{'per':\"{ 'id': 1, 'name': '張三', 'sex': '男' }\"}";
            $.ajax({
                type: "post",
                url: "JsonObject.asmx/GetPersonByObject",
                data: person,
                dataType: 'json',
                contentType: 'application/json;charset=utf-8',
                success: function (data) {
                    if (data.d == "1") {
                        $("#hello").text("服務器接收成功!");
                    }
                    else {
                        $("#hello").text("服務器接收數據失敗!");
                    }
                },
                error: function () {
                    $("#hello").text("程序運行出錯!");
                }
            });
2.後臺
        [WebMethod]
        public string GetPersonByObject(string per)
        {
          Person person=   per.JsonDeserialezer<Person>();//將json反序列化
          if (person.Id == 1)
          {
              return "1";
          }
            return "0";
        }
 
三。List類型json提交,post方式
1.前臺
            //複雜json對象提交2
            var person = "{'per':\"[{ 'id': 1, 'name': '張三', 'sex': '男' },{ 'id': 2, 'name': '王芳', 'sex': '女' }]\"}";
            $.ajax({
                type: "post",
                url: "JsonObject.asmx/GetPersonByOjects",
                data: person,
                dataType: 'json',
                contentType: 'application/json;charset=utf-8',
                success: function (data) {
                        $("#hello").text("就收前臺數據人數:"+data.d);
                },
                error: function () {
                    $("#hello").text("程序運行出錯!");
                }
            });
2.後臺
        [WebMethod]
        public int GetPersonByOjects(string per)
        {
            List<Person> list = per.JsonDeserialezer<List<Person>>();//反序列化json字符串
            return list.Count;
        }
 
四。List類型json提交,get方式
1.前臺
            var person = {'per':"[{ 'id': 1, 'name': '張三', 'sex': '男' },{ 'id': 2, 'name': '王芳', 'sex': '女' }]"};
            $.ajax({
                type: "get",
                url: "JsonObject.asmx/GetPersonByOjects",
                data: person,
                dataType: 'json',
                contentType: 'application/json;charset=utf-8',
                success: function (data) {
                        $("#hello").text("就收前臺數據人數:"+data.d);
                },
                error: function () {
                    $("#hello").text("程序運行出錯!");
                }
            });
2.後臺
        [WebMethod]
        [ScriptMethod(ResponseFormat=ResponseFormat.Json,UseHttpGet=true)]
        public int GetPersonByOjects()
        {
            string per = HttpContext.Current.Request["per"];
            List<Person> list = per.JsonDeserialezer<List<Person>>();
            return list.Count;
        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章