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

-- ::|  分类: asp.net |举报|字号 订阅
. 发送请求的WebForm1.aspx <%@ 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>
view plaincopy to clipboardprint? <%@ 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>
<%@ 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>
. 提供数据的WebForm2.aspx view plaincopy to clipboardprint? 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 = -;
Response.Clear();
Response.ContentEncoding = Encoding.UTF8;
Response.ContentType = "application/json";
Response.Write(result);
Response.Flush();
Response.End();
}
}
view plaincopy to clipboardprint? 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 = -;
Response.Clear();
Response.ContentEncoding = Encoding.UTF8;
Response.ContentType = "application/json";
Response.Write(result);
Response.Flush();
Response.End();
}
}
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 = -; Response.Clear(); Response.ContentEncoding = Encoding.UTF8; Response.ContentType = "application/json"; Response.Write(result); Response.Flush(); Response.End(); } }
注意返回数据的格式 string.Format("{0}({1})", callback, data)

aspx返回json数据的更多相关文章

  1. mvc使用JsonResult返回Json数据

    mvc使用JsonResult返回Json数据   controller 中定义以下方法: public JsonResult UpdateSingle(int id, string actionNa ...

  2. Web API返回JSON数据

    对Web API新手来说,不要忽略了ApiController 在web API中,方法的返回值如果是实体的话实际上是自动返回JSON数据的例如: 他的返回值就是这样的: { "Conten ...

  3. Spring MVC 4.1.4 RESTFUL风格返回JSON数据406错误处理

    Spring MVC 4.1.4 RESTFUL风格返回JSON数据406错误处理 今天在使用spring4.1.4,使用ResponseBody注解返回JSON格式的数据的时候遇到406错误. 解决 ...

  4. 深入了解Struts2返回JSON数据的原理

    首先来看一下JSON官方对于"JSON"的解释: JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.易于人阅读和编写.同时也易于机器解析 ...

  5. SpringMVC——返回JSON数据&&文件上传下载

    --------------------------------------------返回JSON数据------------------------------------------------ ...

  6. 用ajax获取后台数据,返回json数据,怎么在前台使用?

    用ajax获取后台数据,返回json数据,怎么在前台使用呢?后台 C# code   ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 if (dataType == &qu ...

  7. 如何在Crystal Portlet中正确返回JSON数据给AJAX请求?

    当Crystal Portlet中需要采用Ajax请求,并让后台返回Json数据时,如何才能正确.方便的返回Json数据呢? 以下两种方法均可: 方法一:Ajax请求时,采用RenderURL,对应P ...

  8. struts2 的验证框架validation如何返回json数据 以方便ajax交互

    struts2 的验证框架validation简单,好用,但是input只能输出到jsp页面通过struts2的标签<s:fielderror  />才能取出,(EL应该也可以). 如果使 ...

  9. Atitit.列表页面and条件查询的实现最佳实践(1)------设置查询条件and提交查询and返回json数据

    Atitit.列表页面and条件查询的实现最佳实践(1)------设置查询条件and提交查询and返回json数据 1. 1. 配置条件字段@Conditional 1 1 2. 2. 配置条件字段 ...

随机推荐

  1. TCP/IP协议原理与应用笔记27:网际协议(IP)之 选项(Options)

    1. 选项(Options) (1)作用:网络测试或者调试,可选 (2)格式:0~40 bytes 2. 选项类型:

  2. Java Lock

    JVM中的另一种锁Lock的实现.与synchronized不同的是,Lock完全用Java写成,在java这个层面是无关JVM实现的.在java.util.concurrent.locks包中有很多 ...

  3. find中的 time 参数

    find手册中对time的解释:以 atime 为例: -atime n File was last accessed n*24 hours ago. When find figures out ho ...

  4. React Native视频播放(iOS)

    网站链接:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/learn-react-native-video/ React Nativ ...

  5. orcale 列改为大字段

    --添加临时列ALTER TABLE MPD_TASK_LIST ADD(  CLOB_TEMP clob);--数据拷贝到临时列update MPD_TASK_LIST set CLOB_TEMP ...

  6. 从模态视图push到另一个视图

    //需要给模态视图创建一个Nav,然后再调用presentViewController if (_loginVC == nil) { _loginVC = [[LoginViewController ...

  7. 1369 xth 砍树

    1369 xth 砍树  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解       题目描述 Description 在一个凉爽的夏夜,xth 和 ...

  8. HttpContext 讲解

    HttpContext类:封装有关个别HTTP请求的所有HTTP特定的信息,又叫上下文.看到这个解释,我觉得有些抽象,Http特定信息具体又是什么?看了下备注:为继承 IHttpModule 和 IH ...

  9. Houdini FX 14 重磅推出!(附下载方式)

    把之前发布在新浪的博客搬过来了,新浪广告太多,影响阅读和观感,博客园很清净~ SideFX于2015年1月在官网发布Houdini FX 14,喜爱尝鲜.充满好奇心的我迫不及待的装上Apprentic ...

  10. sql深入理解

    我们做软件开发的,大部分人都离不开跟数据库打交道,特别是erp开发的,跟数据库打交道更是频繁,存储过程动不动就是上千行,如果数据量大,人员流动大,那么我们还能保证下一段时间系统还能流畅的运行吗?我们还 ...