码迷,mamicode.com
首页 > Web开发 > 详细

jquery $.post 返回json数据

时间:2014-12-05 12:19:59      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   ar   color   os   sp   for   java   

$(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;
        }
    }

}

 

jquery $.post 返回json数据

标签:style   blog   io   ar   color   os   sp   for   java   

原文地址:http://www.cnblogs.com/flywing/p/4146341.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!