zoukankan      html  css  js  c++  java
  • aspx返回json数据

    JQuery.getJSON 从aspx页面返回JSON数据 .  
    
    2012-01-05 12:44:02|  分类: asp.net |举报|字号 订阅
    1. 发送请求的WebForm1.aspx
    
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Benq.Flower.WebAdmin.Module.WebForm1" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    <script src="../javascript/jquery-1.2.3.pack.js" type="text/javascript" language="javascript"></script>
    <script type="text/javascript" language="javascript">
            function getData() 
            { 
                $.getJSON("WebForm2.aspx?jsoncallback=?", 
                    function(data) 
                    { 
                        $.each(data.items, function(i, item) 
                        { 
                            $("<div></div>") 
                                .text(item.title) 
                                .css("color", item.color) 
                                .appendTo($("#listbox")); 
                        });                     
                    } 
              ); 
            } 
    </script>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <input id="Button1" type="button" value="click to get Json" onclick="javaScript:getData();" />
    </div>
    <div id="listbox">
    </div>
    </form>
    </body>
    </html>
    view plaincopy to clipboardprint?
    
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Benq.Flower.WebAdmin.Module.WebForm1" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    <script src="../javascript/jquery-1.2.3.pack.js" type="text/javascript" language="javascript"></script>
    <script type="text/javascript" language="javascript">
            function getData() 
            { 
                $.getJSON("WebForm2.aspx?jsoncallback=?", 
                    function(data) 
                    { 
                        $.each(data.items, function(i, item) 
                        { 
                            $("<div></div>") 
                                .text(item.title) 
                                .css("color", item.color) 
                                .appendTo($("#listbox")); 
                        });                     
                    } 
              ); 
            } 
    </script>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <input id="Button1" type="button" value="click to get Json" onclick="javaScript:getData();" />
    </div>
    <div id="listbox">
    </div>
    </form>
    </body>
    </html>
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Benq.Flower.WebAdmin.Module.WebForm1" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script src="../javascript/jquery-1.2.3.pack.js" type="text/javascript" language="javascript"></script> <script type="text/javascript" language="javascript"> function getData() { $.getJSON("WebForm2.aspx?jsoncallback=?", function(data) { $.each(data.items, function(i, item) { $("<div></div>") .text(item.title) .css("color", item.color) .appendTo($("#listbox")); }); } ); } </script> </head> <body> <form id="form1" runat="server"> <div> <input id="Button1" type="button" value="click to get Json" onclick="javaScript:getData();" /> </div> <div id="listbox"> </div> </form> </body> </html> 
    2. 提供数据的WebForm2.aspx
    
    view plaincopy to clipboardprint?
    
    public partial class WebForm2 : System.Web.UI.Page 
        { 
    protected void Page_Load(object sender, EventArgs e) 
            { 
    string callback = Request.QueryString["jsoncallback"]; 
    string data = "{"title": "Recent Uploads tagged cat","link": "http://www.sina.com.cn","items": [{"title": "Russell 003","color": "red"},{"title": "Cat [07.04.11]","color": "yellow"}]}"; 
    string result = string.Format("{0}({1})", callback, data); 
                Response.Expires = -1; 
                Response.Clear(); 
                Response.ContentEncoding = Encoding.UTF8; 
                Response.ContentType = "application/json"; 
                Response.Write(result); 
                Response.Flush(); 
                Response.End(); 
            } 
        } 
    view plaincopy to clipboardprint?
    
    public partial class WebForm2 : System.Web.UI.Page 
        { 
    protected void Page_Load(object sender, EventArgs e) 
            { 
    string callback = Request.QueryString["jsoncallback"]; 
    string data = "{"title": "Recent Uploads tagged cat","link": "http://www.sina.com.cn","items": [{"title": "Russell 003","color": "red"},{"title": "Cat [07.04.11]","color": "yellow"}]}"; 
    string result = string.Format("{0}({1})", callback, data); 
                Response.Expires = -1; 
                Response.Clear(); 
                Response.ContentEncoding = Encoding.UTF8; 
                Response.ContentType = "application/json"; 
                Response.Write(result); 
                Response.Flush(); 
                Response.End(); 
            } 
        } 
    public partial class WebForm2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string callback = Request.QueryString["jsoncallback"]; string data = "{"title": "Recent Uploads tagged cat","link": "http://www.sina.com.cn","items": [{"title": "Russell 003","color": "red"},{"title": "Cat [07.04.11]","color": "yellow"}]}"; string result = string.Format("{0}({1})", callback, data); Response.Expires = -1; Response.Clear(); Response.ContentEncoding = Encoding.UTF8; Response.ContentType = "application/json"; Response.Write(result); Response.Flush(); Response.End(); } } 
    注意返回数据的格式 string.Format("{0}({1})", callback, data)
  • 相关阅读:
    一个openbsd的服务器,可以使用它的公网IP~
    深入浅出C++虚函数表
    C++函数的高级特性-函数重载
    《用TCP/IP进行网际互联》读书笔记
    C++中const的作用【转】
    C++学习继承中的作用域(10)
    ping 127.0.0.1与本机的IP有什么不同
    C++多态性:虚函数的调用原理
    mysql连接字段
    常用正则表达式
  • 原文地址:https://www.cnblogs.com/zhao123/p/3596953.html
Copyright © 2011-2022 走看看