JQuery+AJAX使用JSON返回資料(1/2)

來源:互聯網
上載者:User

ajax響應處理頁面 processpage.asp教程x.cs

    

  using system.web.script.serialization;
using modelgroup.model;

public partial class admin_processpage : system.web.ui.page
{
    網頁特效serializer serializer = new javascriptserializer();
    protected void page_load(object sender, eventargs e)
    {
        string name = request.querystring["txt_search"].tostring();
        if (name.equals("aaa")) {
            list<zen_tag> zentag = new list<zen_tag>();
            zentag.add(new zen_tag {
                tag_id = 1,
                tag_name = "nicholas",
                url_show = "nicholas",
                first_letter = "n",
                count = 500,
                create_time = "2009-10-10 10:10"
            });
            string jsonstr = serializer.serialize(zentag);
            response.clear();
            response.write(jsonstr);
            response.end();
        }
    }
}

ajax請求頁面 tagmanagement.aspx

 

     <script type="text/javascript" language="javascript">
        $(document).ready(function() {
            $("#btn_search").click(function() {
                $.ajax({
                    url: "processpage.aspx",
                    type: "get",
                    datatype: "json",
                    data: "txt_search=" + escape($("#txt_search").val()),
                    success: function(data) {
                        alert(data[0].tag_name+"---"+data[0].count);
                    },
                    error: function() { alert("o no~~~"); }
                });
            });
        });
    </script>


asp.net教程 3.5已支援json了,在其擴充中system.web.extensions.dll。

      using system.web.script.serialization;首先引用該命名空間!該命名空間內的serialize()方法可以實現將資料對象序列化為json格式。

      舉例,如果我現在有個實體類object 或數組 arrayinfo 需要序列化為json格式,那麼只需要調用serialize()方法即可


下面來看一詳細關於jquery json返回資料分析與執行個體

jquery通過post傳遞資料到伺服器(不使用外掛程式):

<html>
<head>
    <title>untitled page</title>
    <script language="javascript" type="text/javascript" src="js/jquery-1.3.1.js"></script>
    <script language="javascript" type="text/javascript" src="js/json2.js"></script>
    <script language="javascript" type="text/javascript">
        $(document).ready(function(){
            $("#nameedit").hide();
            $("#aedit").click(function(){
                var value=$("#nameview").text();
                //alert(value);
                $("#txtrename").val(value);
                $("#aedit").empty();
                $("#nameview").hide();
                $("#nameedit").show();
            });
            $("#btnsave").click(function(){
            var value=$("#txtrename").val();
            var obj=createobj(value);
            var objstr=json.stringify(obj);
                $.post("savedata.aspx",objstr,function(response){//使用post傳遞資料,參數:url,資料,返回時處理的函數
                    $("#nameview").text(response);
                    $("#aedit").text("edit").show();
                    $("#nameview").show();
                    $("#nameedit").hide();
                });
            });
          
            function createobj(v)//用於建立json對象
            {
                var test=new object();
                test.newname=v;
                test.data=getdata();//
                return test;
            }
          
            function getdata()
            {
                var data=new object();
                data.first="fff";
                data.second="sss";
                return data;
            }
          
            $("#btncancel").click(function(){
                $("#aedit").text("edit").show();
                $("#nameview").show();
                $("#nameedit").hide();
            });
        });
    </script>
</head>
<body>
    <div id="showdiv">
        <div id="aeditdiv" style="background-color: #afadae; width: 200px;">
            <a id="aedit">edit</a>&nbsp;</div>
        <div id="nameview">
            fnsndgfn</div>
        <div id="nameedit">
            <input type="text" id="txtrename" /><input id="btnsave" type="button" value="save" /><input
                id="btncancel" type="button" value="cancel" />
        </div>
    </div>
</body>
</html>

 

首頁 1 2 末頁
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.