Asp.Net MVC2.0 Jquery Json
3
3
4 <script type="text/javascript">
5 function postBBSData() {
6 var da = $("#bbscontent").val();
7 $.ajax({
8 type: "GET",
9 url: "/home/GetBBSJsonData",//MVC2.0中 Controller:Home,Action:GetBBSJsonData
10 data: "txt=" + da + "&pid=bb&userid=gxw",//运用的GET类型,也可以用POST,只需要底层代码修改下
11 dataType: "json", //返回值为JSON了性
12 success: function (msg) {
13 var p = "<span>" + msg.UserID + "</span><span>" + msg.Context + "</span>"
14 $("#BBSTr").appendTo(p);
15 }
16 });
17 }
18 </script>
19
20 <div id="BBSTr">
21 <span>11111</span>
22 </div>
23 <table>
24 <tr>
25 <td>
26 用户名:<%=Html.TextBox("bbsuserid") %>
27 </td>
28 </tr>
29 <tr>
30 <td>
31 内容:<%=Html.TextArea("bbscontent")%>
32 </td>
33 </tr>
34 <tr>
35 <td>
36 <input type="submit" onclick="postBBSData();" value="提交" />
37 </td>
38 </tr>
39 </table>
40
41
42 Controller:
43
44 public JsonResult GetBBSJsonData()
45 {
46 string context = Request.QueryString["txt"].ToString();
47 string productID = Request.QueryString["pid"].ToString();
48 string userId = Request.QueryString["userid"].ToString();
49 var p = new//这也是一种类型的JSON
50 {
51 Context = context,
52 UserID = userId,
53 CurrentTime = DateTime.Now.ToString()
54 };
55 return Json(p, JsonRequestBehavior.AllowGet);//JsonRequestBehavior.AllowGet为了Get传值不出错
56 }