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

1. 发送请求的WebForm1.aspx

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

view plaincopy to clipboardprint?

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

  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.     } 

view plaincopy to clipboardprint?

  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.     } 

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)

原文地址:https://www.cnblogs.com/fogwang/p/2666598.html