jquery $.post 返回json数据

$(function () {
            $("#prompt").hide();
            $("#searchIpt").keyup(function () {
                var key = $("#searchIpt").val();
                if (key != undefined && key != null && key.length > 0) {

                    $.post("Query.aspx", { "key": key, "flag": "search" }, function (data, status) {
                        if (status = "success") {
                            var html = "";
                              for (var i = 0; i < data.length; i++) {
                                  html += " <div class=‘skey‘>" + data[i].Name + "</div>";
                              }
                            var $prompt = $("#prompt");
                            var searchIpt = $("#searchIpt");
                            var hleft = searchIpt.offset().left;
                            var htop = searchIpt.offset().top;
                            $prompt.css({ width: searchIpt.width() + 4, left: hleft - 8, top: htop - 8 });
                            $prompt.html("").append(html).show();

                        }
                        else {
                            $prompt.hide();
                        }

                    },"json");
                }
                $("#prompt").hide();
            });
    });

<div id="prompt" style="background-color: #f1f1f1; position: relative; z-index: 100;top: 0px; left: 0px;">
  <div class="skey">
    2</div>
  <div class="skey">
    3</div>
</div>

protected void Page_Load(object sender, EventArgs e)
    {
        string flag = Request.Form["flag"];
        if (!string.IsNullOrEmpty(flag))
        {
            switch (flag)
            {
                case "search":
                    string key = Request.Form["key"];

                    LoadData(key);
                    break;

                case "save":
                    string text1 = Request.Form["text1"];
                    string text2 = Request.Form["text2"];
                    string text3 = Request.Form["text3"];
                    //这里获取数据保存

                    //这里返回给前台
                    Response.Write("提交到后台接受保存啦");

                    Response.End();
                    break;
            }
        }
    }

    public void LoadData(string text)
    {
        List<Person> nameList = new List<Person>();
        Person p = new Person();
        p.Name = "张三";
        p.Id = 1;
        nameList.Add(p);
        p = new Person();
        p.Name = "李四";
        p.Id = 2;
        nameList.Add(p);
        p = new Person();
        p.Name = "张四";
        p.Id = 3;
        nameList.Add(p);
        p = new Person();
        p.Name = "王五";
        p.Id = 4;
        nameList.Add(p);
        List<Person> collection = new List<Person>();
        foreach (Person pe in nameList)
        {
            if (pe.Name.IndexOf(text) != -1)
            {
                collection.Add(pe);
            }
        }
        
        JavaScriptSerializer j = new JavaScriptSerializer();
        string json = j.Serialize(collection);//转换成json格式字符串
        Response.Write(json);
        Response.End();
    }
public class Person
{
    private string _name;
    public string Name
    {
        get
        {
            return _name;
        }
        set
        {
            _name = value;
        }
    }

    private int _id;
    public int Id
    {
        get
        {
            return _id;
        }
        set
        {
            _id = value;
        }
    }

}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。