jquery $.post 返回json資料

來源:互聯網
上載者:User

標籤: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資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.