==============================================================html页面================================
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="jquery-1.10.2.js"></script>
<script type="text/javascript">
$(function () {
$("#btnGetJson").click(
function () {
$.getJSON("ResponseJson.ashx", "a=3&b=4", function (data) {
alert(data[1].CityName);
});
});
$("#btnJQGet").click(function () {
$.get("ResponseJson.ashx", "aa=22", function (data) {
alert(data);
});
});
$("#btnJQPost").click(function () {
$.post("ResponseJson.ashx", "bb=22", function (data) {
alert(data[0].CityName);
}, "json")
});
$("#btnAjax").click(
function () {
$.ajax({
url: "ResponseJson.ashx",
data: "a=3",
type: "Post",
success: function (data) {
//alert(data);
$("#divDemo").text(data);
},
error: function () {
alert("错误!");
}
});
}
);
$("#btnLoad").click(function () {
$("#divDemo").load("ResponseJson.ashx", { id: 333 }, function (data) {
alert(data);
});
});
});
</script>
</head>
<body>
<input id="btnGetJson" type="button" value="JQ GetJson" />
<input type="button" value="JQ Get" id="btnJQGet" />
<input type="button" value="JQ Post" id="btnJQPost" />
<input type="button" value="JQ ajax" id="btnAjax" />
<input type="button" value="JQ load" id="btnLoad" />
<div id="divDemo">
</div>
</body>
</html>
===========================================================================ashx================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
namespace _20141224.Ajax
{
/// <summary>
/// Summary description for ResponseJson
/// </summary>
public class ResponseJson : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
System.Collections.Generic.List<CityInfo> cities = new List<CityInfo>() {
new CityInfo() { CityId = 1, CityName = "郑州" },
new CityInfo() { CityId = 2, CityName = "周口" },
new CityInfo() { CityId = 3, CityName = "漯河" },
new CityInfo() { CityId = 4, CityName = "驻马店" },
new CityInfo() { CityId = 5, CityName = "许昌" },
new CityInfo() { CityId = 6, CityName = "开封" },
new CityInfo() { CityId = 7, CityName = "巩义" }
};
//StringBuilder sb = new StringBuilder();
//sb.Append("[");
//foreach (var CityInfo in cities)
//{
// sb.Append("{");
// sb.AppendFormat("\"CityId\":\"{0}\",\"CityName\":\"{1}\"", CityInfo.CityId,CityInfo.CityName);
// sb.Append("},");
//}
//string str = sb.ToString().TrimEnd(',');
//str += "]";
System.Web.Script.Serialization.JavaScriptSerializer javaScriptSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
string json = javaScriptSerializer.Serialize(cities);
context.Response.Write(json);
}
public bool IsReusable
{
get
{
return false;
}
}
public class CityInfo
{
public string CityName { get; set; }
public int CityId { get; set; }
}
}
}
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script src="jquery-1.10.2.js"></script>
<script type="text/javascript">
$(function () {
$("#btnGetJson").click(
function () {
$.getJSON("ResponseJson.ashx", "a=3&b=4", function (data) {
alert(data[1].CityName);
});
});
$("#btnJQGet").click(function () {
$.get("ResponseJson.ashx", "aa=22", function (data) {
alert(data);
});
});
$("#btnJQPost").click(function () {
$.post("ResponseJson.ashx", "bb=22", function (data) {
alert(data[0].CityName);
}, "json")
});
$("#btnAjax").click(
function () {
$.ajax({
url: "ResponseJson.ashx",
data: "a=3",
type: "Post",
success: function (data) {
//alert(data);
$("#divDemo").text(data);
},
error: function () {
alert("错误!");
}
});
}
);
$("#btnLoad").click(function () {
$("#divDemo").load("ResponseJson.ashx", { id: 333 }, function (data) {
alert(data);
});
});
});
</script>
</head>
<body>
<input id="btnGetJson" type="button" value="JQ GetJson" />
<input type="button" value="JQ Get" id="btnJQGet" />
<input type="button" value="JQ Post" id="btnJQPost" />
<input type="button" value="JQ ajax" id="btnAjax" />
<input type="button" value="JQ load" id="btnLoad" />
<div id="divDemo">
</div>
</body>
</html>
===========================================================================ashx================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
namespace _20141224.Ajax
{
/// <summary>
/// Summary description for ResponseJson
/// </summary>
public class ResponseJson : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
System.Collections.Generic.List<CityInfo> cities = new List<CityInfo>() {
new CityInfo() { CityId = 1, CityName = "郑州" },
new CityInfo() { CityId = 2, CityName = "周口" },
new CityInfo() { CityId = 3, CityName = "漯河" },
new CityInfo() { CityId = 4, CityName = "驻马店" },
new CityInfo() { CityId = 5, CityName = "许昌" },
new CityInfo() { CityId = 6, CityName = "开封" },
new CityInfo() { CityId = 7, CityName = "巩义" }
};
//StringBuilder sb = new StringBuilder();
//sb.Append("[");
//foreach (var CityInfo in cities)
//{
// sb.Append("{");
// sb.AppendFormat("\"CityId\":\"{0}\",\"CityName\":\"{1}\"", CityInfo.CityId,CityInfo.CityName);
// sb.Append("},");
//}
//string str = sb.ToString().TrimEnd(',');
//str += "]";
System.Web.Script.Serialization.JavaScriptSerializer javaScriptSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
string json = javaScriptSerializer.Serialize(cities);
context.Response.Write(json);
}
public bool IsReusable
{
get
{
return false;
}
}
public class CityInfo
{
public string CityName { get; set; }
public int CityId { get; set; }
}
}
}