代码改变世界

ajax返回值

  sunice  阅读(203)  评论(0编辑  收藏  举报

前端:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <script src="~/Scripts/jquery-1.8.2.js"></script>
    <script>
        $(function () {
            $("#btn").click(function () {
                $.ajax({
                    url: "/Home/Test",
                    data: {},
                    dataType: "json",
                    success: function (data) {
                        for (var s in data) {
                            $("#txt" + s).val(data[s]);
                        }
                        
                    }
                })
            })
        })
    </script>
</head>
 
<body>
    <div>
        <input type="text" id="txtName" /><br />
        <input type="text" id="txtPwd" /><br />
        <button id="btn">提交</button>
    </div>
</body>
</html>

  后端:

1
2
3
4
5
6
7
8
9
10
public ActionResult Test()
   {
       Student stu = new Student()
       {
           Name="王大",
           Pwd="123456"
       };
       
       return Json(stu, JsonRequestBehavior.AllowGet);
   }

  

点击右上角即可分享
微信分享提示