jquery ajax返回值类型为json的练习
作者:feng 日期:2010-09-07
前台页面 Default.aspx :
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Text_Default" %>
<!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>JSON练习</title>
<script src="../components/jquery/jquery-1[1].3.2.min.js" type="text/javascript"></script> <!--引用 jquery类库-->
<script type="text/javascript">
$(function()
{
$("#bt1").click(function()
{
$.ajax({
url: "Default.aspx?id=1",
type: "get",
dataType: 'json',
cache: false,
beforeSend: function(result){
},
success: function(result){
var html='';
$(result).each(function(i) //解析方法1
{
html+=i+"---"+this.name+":"+this.value;
alert(i+"---"+this.name+":"+this.value);
});
alert(html);
},
error: function(result, status) {
if (status == 'error') {
alert("系统发生错误");
}
}
});
});
$("#bt2").click(function()
{
$.ajax({
url: "Default.aspx?pid=2",
type: "get",
dataType: 'json',
cache: false,
beforeSend: function(result){
},
success: function(result){
alert(result.name);
alert(result.value);
alert(result.name+":"+result.value);
},
error: function(result, status) {
if (status == 'error') {
alert("系统发生错误");
}
}
});
});
$("#bt3").click(function()
{
$.ajax({
url: "Default.aspx?id=1",
type: "get",
dataType: 'json',
cache: false,
beforeSend: function(result){
},
success: function(result){
var html='';
$.each(result,function(i,comment) //解析方法2
{
html+=i+"---"+comment['name']+":"+comment['value'];
alert(i+"---"+comment['name']+":"+comment['value']);
});
alert(html);
},
error: function(result, status) {
if (status == 'error') {
alert("系统发生错误");
}
}
});
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="bt1" type="button" value="点击返回json集合" />
<input id="bt2" type="button" value="点击返回一个json字符串" />
<input id="bt3" type="button" value="点击返回json集合(解析方法不一样)" />
</div>
</form>
</body>
</html>
后台代码 Default.aspx.cs :
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Data.SqlClient;
public partial class Text_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.QueryString["id"] != null)
{
StringBuilder str = new StringBuilder();
for (int i = 0; i < 4; i++)
{
str.AppendFormat("{{name:\"{0}\",value:\"{1}\"}},", "ssss" + i.ToString(), i.ToString());
}
Response.Write("[" + str.ToString().TrimEnd(',') + "]"); //去掉最后的 ','返回一个json字符串
Response.End();
}
if (Request.QueryString["pid"] != null)
{
StringBuilder str = new StringBuilder();
str.AppendFormat("{{name:\"{0}\",value:\"{1}\"}}", "ssss5", "5");
Response.Write(str.ToString());
Response.End();
}
JScript.Alert(tt1.Value);
}
}
//连接数据库。用数据库的内容填充。
//private string GetDepotjson()
//{
// StringBuilder jsonstr = new StringBuilder();
// SqlDataReader dr = bd.GetList(""); //读取数据库,返回SqlDataReader
// while (dr.Read())
// {
// jsonstr.AppendFormat("{{name:\"{0}\",value:\"{1}\"}},", dr["Tname"], dr["TID"]);
// }
// dr.Close(); //关闭
// return "[" + jsonstr.ToString().TrimEnd(',') + "]"; //返回一个json字符串
//}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Text_Default" %>
<!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>JSON练习</title>
<script src="../components/jquery/jquery-1[1].3.2.min.js" type="text/javascript"></script> <!--引用 jquery类库-->
<script type="text/javascript">
$(function()
{
$("#bt1").click(function()
{
$.ajax({
url: "Default.aspx?id=1",
type: "get",
dataType: 'json',
cache: false,
beforeSend: function(result){
},
success: function(result){
var html='';
$(result).each(function(i) //解析方法1
{
html+=i+"---"+this.name+":"+this.value;
alert(i+"---"+this.name+":"+this.value);
});
alert(html);
},
error: function(result, status) {
if (status == 'error') {
alert("系统发生错误");
}
}
});
});
$("#bt2").click(function()
{
$.ajax({
url: "Default.aspx?pid=2",
type: "get",
dataType: 'json',
cache: false,
beforeSend: function(result){
},
success: function(result){
alert(result.name);
alert(result.value);
alert(result.name+":"+result.value);
},
error: function(result, status) {
if (status == 'error') {
alert("系统发生错误");
}
}
});
});
$("#bt3").click(function()
{
$.ajax({
url: "Default.aspx?id=1",
type: "get",
dataType: 'json',
cache: false,
beforeSend: function(result){
},
success: function(result){
var html='';
$.each(result,function(i,comment) //解析方法2
{
html+=i+"---"+comment['name']+":"+comment['value'];
alert(i+"---"+comment['name']+":"+comment['value']);
});
alert(html);
},
error: function(result, status) {
if (status == 'error') {
alert("系统发生错误");
}
}
});
});
});
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="bt1" type="button" value="点击返回json集合" />
<input id="bt2" type="button" value="点击返回一个json字符串" />
<input id="bt3" type="button" value="点击返回json集合(解析方法不一样)" />
</div>
</form>
</body>
</html>
后台代码 Default.aspx.cs :
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Data.SqlClient;
public partial class Text_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.QueryString["id"] != null)
{
StringBuilder str = new StringBuilder();
for (int i = 0; i < 4; i++)
{
str.AppendFormat("{{name:\"{0}\",value:\"{1}\"}},", "ssss" + i.ToString(), i.ToString());
}
Response.Write("[" + str.ToString().TrimEnd(',') + "]"); //去掉最后的 ','返回一个json字符串
Response.End();
}
if (Request.QueryString["pid"] != null)
{
StringBuilder str = new StringBuilder();
str.AppendFormat("{{name:\"{0}\",value:\"{1}\"}}", "ssss5", "5");
Response.Write(str.ToString());
Response.End();
}
JScript.Alert(tt1.Value);
}
}
//连接数据库。用数据库的内容填充。
//private string GetDepotjson()
//{
// StringBuilder jsonstr = new StringBuilder();
// SqlDataReader dr = bd.GetList(""); //读取数据库,返回SqlDataReader
// while (dr.Read())
// {
// jsonstr.AppendFormat("{{name:\"{0}\",value:\"{1}\"}},", dr["Tname"], dr["TID"]);
// }
// dr.Close(); //关闭
// return "[" + jsonstr.ToString().TrimEnd(',') + "]"; //返回一个json字符串
//}
}
评论: 0 | 引用: 0 | 查看次数: 1877
发表评论
上一篇
下一篇

文章来自:
Tags: 






