1.
<script type="text/javascript">
$(document).ready(function () {
$("#button").click(function () {
$.ajax({
type: "post",
url: "Service1.asmx/GetDataset",
contentType: "application/json; charset=utf-8",
dataType: "json",
dataFilter: function (data) {
//debugger
alert(data);
var msg;
if (typeof (JSON) !== 'undefined' && typeof (JSON.parse) === 'function') {
msg = JSON.parse(data);
} else {
msg = eval('(' + data + ')');
}
if (msg.hasOwnProperty('d')) {
return msg.d;
} else {
return msg;
}
},
success: function (data) {
//debugger
alert(data);
$("#groupName").text(data.groupName);
$("#memberList").text(data.memberList);
}
});
});
});
</script>
2.
// ASP.NET AJAX를 사용하여 스크립트에서 이 웹 서비스를 호출하려면 다음 줄의 주석 처리를 제거합니다.
[System.Web.Script.Services.ScriptService]
.
.
[WebMethod]
public string GetDataset()
{
DataSet ds = GetDS();
JavaScriptSerializer jss = new JavaScriptSerializer();
DataSetConverter dsc = new DataSetConverter();
IDictionary<string, object> retVal = dsc.Serialize(ds, jss);
string result = jss.Serialize(retVal);
return result;
}
private DataSet GetDS()
{
DataSet ds;
VTFramework.DB.vCS_DB_MSSQL db = new VTFramework.DB.vCS_DB_MSSQL("data source='localhost';initial catalog='REQUEST_MANAGE';password='simple2009';user id='sa';");
db.mLoad();
ds = db.mGetEx("SELECT * from [REQUEST_MANAGE].[dbo].[USER_MANAGE]");
db.mDispose();
return ds;
}
3.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Data;
using System.Collections;
using System.Collections.ObjectModel;
namespace AjaxWithJson
{
public class DataSetConverter : JavaScriptConverter
{
public override IEnumerable<Type> SupportedTypes
{
//Define the DataTable as a supported type.
get { return new ReadOnlyCollection<Type>(new List<Type>(new Type[] { typeof(DataSet) })); }
}
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer)
{
Dictionary<string, object> dtDic = new Dictionary<string, object>();
DataSet ds = obj as DataSet;
foreach (DataTable dt in ds.Tables)
{
// Create the representation.
Dictionary<string, object> rowDic = new Dictionary<string, object>();
int i = 0;
foreach (DataRow row in dt.Rows)
{
//Add each entry to the dictionary.
Dictionary<string, object> colDic = new Dictionary<string, object>();
foreach (DataColumn col in row.Table.Columns)
{
colDic.Add(col.ColumnName, row[col]);
}
rowDic.Add("row" + (i++).ToString(), colDic);
}
dtDic.Add(dt.TableName, rowDic);
}
return dtDic;
}
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer)
{
if (dictionary == null)
throw new ArgumentNullException("dictionary");
if (type == typeof(DataTable))
{
// Create the instance to deserialize into.
DataTable list = new DataTable();
// Deserialize the ListItemCollection's items.
ArrayList itemsList = (ArrayList)dictionary["Rows"];
for (int i = 0; i < itemsList.Count; i++)
list.Rows.Add(serializer.ConvertToType<DataRow>(itemsList[i]));
return list;
}
return null;
}
}
}
4. ASHX
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Web.SessionState;
namespace AjaxWithJson
{
/// <summary>
/// ConfirmBitmapHandler의 요약 설명입니다.
/// </summary>
public class ConfirmBitmapHandler : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
// context.Response.ContentType = "text/plain";
// context.Response.Write("Hello World");
context.Response.ContentType = "image/jpeg";
// 이미지 사이즈
int lenX = 80, lenY = 30;
Bitmap bm = new Bitmap( lenX, lenY );
Graphics g = Graphics.FromImage( bm );
// 배경으로 그라데이션 처리
LinearGradientBrush bgGr = new LinearGradientBrush(
new Point(0,0),
new Point(lenX, lenY),
Color.Blue,
Color.Black);
g.FillRectangle( bgGr, 0, 0, lenX, lenY );
// 5자리 숫자의 난수를 발생하여 텍스트를 만든다
string text = string.Empty;
Random rnd = new Random();
for(int i=0; i<5; i++)
{
text += rnd.Next(9).ToString();
}
// 텍스트를 그린다.
Brush textBrush = new SolidBrush( Color.White );
g.DrawString( text, new Font("굴림", 15), textBrush, 10, 5, StringFormat.GenericDefault );
// 스트림에 비트맵을 쓴다.
bm.Save( context.Response.OutputStream, ImageFormat.Jpeg );
// 5자리 숫자를 세션에 담는다.
context.Session["ValidateString"] = text;
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
-------------------------------------------------------