首页 > 技术文章 > js 利用ajax将前台数据传到后台(json格式)

hiytom 2013-08-23 17:00 原文

有两种办法

1.

 1 $(document).ready(function () {
 2         $("#saveAll").live("click", function () {  //点击id为saveAll
 3             $('input:text').each(function (i) {    //找到所有input的type为text的文本框
 4                 $(this).trigger('blur');       //执行所有input的type为text的文本框失去焦点事件
 5             });
 6                         //创建json数组
 7                         var json = "{"
 8                         $("input:text").each(function (i) {
 9                             if (i == 0) {
10                                 json += $(this).attr("id") + ":" + $(this).val();
11                             } else {
12                                 json += "," + $(this).attr("id") + ":" + $(this).val();
13                             }
14                         });
15                         json += "}";
16                         //将json数组传到后台
17                         $.ajax({
18                             type: "Post",
19                             url: "/Exam/Correcting",
20                             data: { jsonmodel: json },
21                             dataType: "json",
22                             success: function (r) {
23                             },
24                             error: function (err) {
25                                 alert("查询失败");
26                             }
27                         });
28 
29             $.post("/Exam/ExamCorrecting", $("form").serialize(), function (r) {
30            });
31            return false;
32         });

后台添加解析传过来的json数据的方法:

 1 /// <summary>
 2         /// 将json数据转化为Object类型
 3         /// </summary>
 4         /// <param name="input"></param>
 5         /// <returns></returns>
 6         public static Object JsonDataToObject(String input)
 7         {
 8             try
 9             {
10                 if (input.Trim().StartsWith("{"))
11                 {
12                     JObject result = JObject.Parse(input);
13                     if (result.Property("errorCode") != null)
14                     {
15                         throw new Exception();
16                     }
17                     return result;
18                 }
19                 else
20                 {
21                     JArray result = JArray.Parse(input);
22                     return result;
23                 }
24             }
25             catch (Exception ex)
26             {
27                 //Logger.Error("解析JSON数据异常:" + ex.Message);
28                 return null;
29             }
30         }

最后遍历传过来的json数据了:

1  public ActionResult Correcting(string jsonmodel)
2         {
3             //解析传过来的json数组
4             JObject obj = (JObject)JsonDataToObject(jsonmodel);
5             foreach (var item in obj)
6             {
7             }
8         }        

这里说一下,解析出来的obj是一个键值对,也就是说,key必须要唯一的,否则会出错.

 

 

 

2(mvc下).

1     $(document).ready(function () {
2             $.post("/Exam/ExamCorrecting", $("form").serialize(), function (r) {
3             });
4             return false;
5         });

将整个表单post过去,FormCollection fc接受json,然后遍历.

fc[item]为key值, v为value值

 1 public ActionResult Correcting(FormCollection fc)
 2         {
 3             foreach (string item in fc)
 4             {
 5                 var v = fc[item];
 6                 if (string.IsNullOrEmpty(v))
 7                 {
 8                     continue;
 9                 }
10                 double score = double.Parse(v);
11                 if (o_Res.updateExamAnswer(item, score))
12                 {
13                 }
14                 else
15                 {
16                     return Json(new MLJsonResult { msg = Resources.Message.SaveFailure, success = false });
17                 }
18             }

 

3.webForm下 js还是同第二个相同

后台好像是Request.Form来接受数据吧,没用过,待百度..

推荐阅读