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. jQery无缝滚动效果

    思路: 赋值所有li,添加到ul末尾,重新计算ul宽度 每次移动一个固定的值,当超出一半时,将ul拉回原位 以下代码 <!DOCTYPE html> <html> <he ...

  2. 【Android 界面效果37】ViewStub的应用

    在开发应用程序的时候,经常会遇到这样的情况,会在运行时动态根据条件来决定显示哪个View或某个布局.那么最通常的想法就是把可能用到的View都写在上面,先把它们的可见性都设为View.GONE,然后在 ...

  3. 浅析vb.net与vb6的不同

    vb6.0与vb.net 都是微软公司推出的编程工具,而vb.net是在vb6的基础上发展而来的,其语法与VB6.0相似,但VB.net并不是VB6.0 的简单升级,而是增加了更多特性,其中重要的一点 ...

  4. 【AR】Vuforia App key is missing.Please get a valid key

    在跑Vuforia 的sample android app 的时候报了下面这个错,找了半天才找到解决方法: "Vuforia App key is missing. Please get a ...

  5. Neither the JAVA_HOME nor the JRE_HOME environment variable is defined

    执行远程shell,启动远程机器的tomat时遇到次错误.后来发现原来是远程机器的.profile被人改掉了! 在.profile里加入 export JAVA_HOME=/home/evans/jd ...

  6. 用pf透明地将流量从一台机器转到另一台机器上的缘起及实现方式对比

    下面是也是我在12580工作时发生的事情,重新记录并发出来.这种特殊需求很考 验PF的功底.在新旧系统并存,做重构的时候有时很需要这种救急的作法.一.缘起miscweb1(172.16.88.228) ...

  7. AspxGridView 主子表设置

    AspxGridView主子表(master-detail)设置共分3步: 第1步: 设置主表允许其显示 DetailRow 放置主GridView到页面, 取名"grvList" ...

  8. HttpContext 讲解

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

  9. Objective-C 【NSString-字符串比较&前后缀检查及搜索】

    ———————————————————————————————————————————NSString 字符串比较 #import <Foundation/Foundation.h> vo ...

  10. python基础:os模块中关于文件/目录常用的函数使用方法

    Python是跨平台的语言,也即是说同样的源代码在不同的操作系统不需要修改就可以同样实现 因此Python的作者就倒腾了OS模块这么一个玩意儿出来,有了OS模块,我们不需要关心什么操作系统下使用什么模 ...