Public Partial classWebForm2:System.Web.UI.Page {protected voidPage_Load (Objectsender, EventArgs e) { stringcallback = request.querystring["Jsoncallback"]; stringdata ="{\ "title\": \ "recent uploads tagged cat\", \ "link\": \ "http://www.sina.com.cn\", \ "items\": [{\ "title\": \ "Russell 003\ ", \" color\ ": \" red\ "},{\" title\ ": \" Cat [07.04.11]\ ", \" color\ ": \" Yellow\ "}]}"; stringresult =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 (); } }
<%@ 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"> "http://www.w3.org/1999/xhtml"> "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> "Form1"runat="Server"> <div> <input id="Button1"Type="Button"Value="Click to get Json"onclick="Javascript:getdata ();"/> </div> <div id="ListBox"> </div> </form> </body>
Jquery.getjson returning JSON data from an ASPX page