JQuery.getJSON 从aspx页面返回JSON数据 .

11 篇文章 0 订阅
 

JQuery的getJSON方法,对于后台是aspx页面,应该如何返回什么格式的数据,相关资料甚是寥寥。经一番艰苦尝试,总算成功。现贴示例如下:


1. 发送请求的WebForm1.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Benq.Flower.WebAdmin.Module.WebForm1" %>  
  2.   
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  4. <html xmlns="http://www.w3.org/1999/xhtml">  
  5. <head runat="server">  
  6.     <title></title>  
  7.   
  8.     <script src="../javascript/jquery-1.2.3.pack.js" type="text/javascript" language="javascript"></script>  
  9.   
  10.     <script type="text/javascript" language="javascript">  
  11.         function getData()  
  12.         {  
  13.             $.getJSON("WebForm2.aspx?jsoncallback=?",  
  14.                 function(data)  
  15.                 {  
  16.                     $.each(data.items, function(i, item)  
  17.                     {  
  18.                         $("<div></div>")  
  19.                             .text(item.title)  
  20.                             .css("color", item.color)  
  21.                             .appendTo($("#listbox"));  
  22.                     });                      
  23.                 }  
  24.           );  
  25.   
  26.         }  
  27.     </script>  
  28.   
  29. </head>  
  30. <body>  
  31.     <form id="form1" runat="server">  
  32.     <div>          
  33.         <input id="Button1" type="button" value="click to get Json" onclick="javaScript:getData();" />  
  34.     </div>  
  35.     <div id="listbox">  
  36.     </div>  
  37.     </form>  
  38. </body>  
  39. </html>  

2. 提供数据的WebForm2.aspx

  1. public partial class WebForm2 : System.Web.UI.Page  
  2.     {  
  3.         protected void Page_Load(object sender, EventArgs e)  
  4.         {  
  5.             string callback = Request.QueryString["jsoncallback"];  
  6.             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\"}]}";  
  7.             string result = string.Format("{0}({1})", callback, data);  
  8.             Response.Expires = -1;  
  9.             Response.Clear();  
  10.             Response.ContentEncoding = Encoding.UTF8;  
  11.             Response.ContentType = "application/json";  
  12.             Response.Write(result);  
  13.             Response.Flush();  
  14.             Response.End();  
  15.         }  
  16.     }  

注意返回数据的格式  string.Format("{0}({1})", callback, data)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值