代码改变世界

ajax返回值

2017-06-03 22:34  sunice  阅读(203)  评论(0编辑  收藏  举报

前端:

<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>

  后端:

     public ActionResult Test()
        {
            Student stu = new Student()
            {
                Name="王大",
                Pwd="123456"
            };
           
            return Json(stu, JsonRequestBehavior.AllowGet);
        }