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

前端之家收集整理的这篇文章主要介绍了JQuery.getJSON 从aspx页面返回JSON数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MyTest.Default" %>

<!DOCTYPE html >  
<html @H_403_19@xmlns="http://www.w3.org/1999/xhtml">  
<head @H_403_19@id="Head1" @H_403_19@runat="server">  
    <title></title>  
    <script @H_403_19@src="https://cdn.bootcss.com/jquery/1.12.4/jquery.js" @H_403_19@type="text/javascript" @H_403_19@language="javascript"></script>  
    <script @H_403_19@type="text/javascript" @H_403_19@language="javascript"> function getData() { $.getJSON("GetJson.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 @H_403_19@id="form1" @H_403_19@runat="server">  
    <div>          
        <input @H_403_19@id="Button1" @H_403_19@type="button" @H_403_19@value="点击获取Json" @H_403_19@onclick="javaScript:getData();" />  
    </div>  
    <div @H_403_19@id="listBox">  
    </div>  
    </form>  
</body>  
</html>

GetJson.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace MyTest
{
    public partial class GetJson : System.Web.UI.Page
    {
        protected void Page_Load(object sender,EventArgs e)
        {
            string callback = Request.QueryString["jsoncallback"];
            string data = "{\"title\": \"咖菲猫\",\"link\": \"http://www.sina.com.cn\",\"items\": [{\"title\": \"罗素003\",\"color\": \"red\"},{\"title\": \"猫\",\"color\": \"Navy\"}]}";
            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(); 
        }
    }
}

运行结果如图:

原文链接:https://www.f2er.com/json/288835.html

猜你在找的Json相关文章