ASP 페에지 코딩 부분
호출 페이지로 전달할 데이터를 생성해 낸다.
호출된 데이터는 JSON 타입으로 사용하기 위해 문자열로 만들어준다.
|
<%
response.charset = "utf-8"
dim cmd, db
set db = server.createobject("adodb.connection")
db.open "provider=sqloledb; data source = IP Address;uid=DBID;pwd=PASSWORD;database=DBNAME"
set cmd = server.createobject("adodb.command")
cmd.activeconnection = db
cmd.commandtext = spNAME
cmd.commandtype = 1
set rs = cmd.execute
if not rs.eof then
OpenEventCommentList = rs.getrows()
end if
rs.close
set rs = nothing
' ============================================================
' 배열로 저장된 레코드 셋 출력하기.
' ============================================================
dim rowCnt, colCnt
rowCnt = ubound(OpenEventCommentList, 2) '행
colCnt = ubound(OpenEventCommentList, 1) '열
dim json
if IsArray(OpenEventCommentList) then
json = json + "["
for i=0 to rowCnt
json = json + "{"
for j=0 to colCnt
if j=0 then json = json + """UserID"":""" end if
if j=1 then json = json + """Content"":""" end if
if j=2 then json = json + """RegDate"":""" end if
json = json + OpenEventCommentList(j, i)
json = json + ""","
next
json = Mid(json, 1, len(json) -1)
json = json + "},"
next
json = Mid(json, 1, len(json) -1)
json = json + "]"
end if
response.write json
%> |
HTML 호출 페이지
1. createXMLHttpRequest() 호출 객체를 생성한다.
2. 콜백함수를 호출할 getCommentList() 객체를 생성한다.
3. 콜백함수 getCommentList_Callback()를 생성한다.
|
<!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>
<title>댓글 기능</title>
<script language="javascript" type="text/javascript">
var strTable = "";
function createXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}
function getCommentList() {
createXMLHttpRequest();
xmlHttp.onreadystatechange = getCommentList_Callback;
xmlHttp.open("GET", "GetCmtList.asp", true);
xmlHttp.send();
}
function getCommentList_Callback() {
if (xmlHttp.readyState == 4) {
if (xmlHttp.status == 200) {
var obj = eval('(' + xmlHttp.responseText + ')');
strTable += "<table border='1' cellpadding='0' cellspacing='0'><tr><td>Idx</td><td>keyword</td><td>writeday</td></tr>";
for (var i = 0; i < obj.length; i++) {
strTable += "<tr>";
strTable += "<td>" + obj[i]["UserID"] + "</td>";
strTable += "<td>" + obj[i]["Content"] + "</td>";
strTable += "<td>" + obj[i]["RegDate"] + "</td>";
strTable += "</tr>";
}
strTable += "</table>";
document.getElementById("CommentList").innerHTML = strTable;
}
}
}
</script>
</head>
<body>
<div id="CommentList"></div>
<form id="eventComment">
<textarea name="content" id="cheerContent" cols="100" rows="3"></textarea>
<input type="button" value="comment" />
</form>
<script type="text/javascript" language="javascript">
getCommentList();
</script>
</body>
</html>
|