加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 18:45:52 所属栏目:百科 来源:网络整理
导读:Default.aspx %@ Page Language= "C#" AutoEventWireup= "true" CodeBehind= "Default.aspx.cs" Inherits= "MyTest.Default" % !DOCTYPE html html xmlns = "http://www.w3.org/1999/xhtml" head id = "Head1" runat = "server" title / title script src =

Default.aspx

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

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

运行结果如图:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读