반응형
Taeyo's ASP.NET AJAX v1.0 책을 예약까지 해서 사놓았는데 이제야 본다..
게으른건지.. 아니면 정말 일에 치였었는지..
간단한 샘플 하나
소스 2-16
defaultJSON.htm
<SCRIPT language=javascript type=text/javascript>
var xmlHttp;
function GetInfo()
{
var empID = document.getElementById("EmpID").value;
var url = "processJSON.aspx?EmpID=" + empID;
xmlHttp = GetXMLHttp();
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = callback;
xmlHttp.send(null);
}
function callback()
{
if(xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
parseAndDisplay(xmlHttp.responseText);
}
else
{
var label = document.getElementById("errorText");
label.style.color = "red";
label.innerHTML = "에러 발생 : " + xmlHttp.statusText;
}
}
}
function parseAndDisplay( data )
{
eval("var emp = " + data);
document.getElementById("LastName").value = emp.LastName;
document.getElementById("FirstName").value = emp.FirstName;
document.getElementById("Title").value = emp.Title;
document.getElementById("BirthDate").value = emp.BirthDate;
document.getElementById("City").value = emp.City;
}
function GetXMLHttp()
{
//Firefox를 위한 코드
if(window.XMLHttpRequest) return new XMLHttpRequest(); // IE 6이하를 위한 코드
var versions = [
"MSXML2.XMLHttp.5.0",
"MSXML2.XMLHttp.4.0",
"MSXML2.XMLHttp.3.0",
"MSXML2.XMLHttp",
"Microsoft.XMLHttp"
];
for(var i = 0; i < versions.length; i++)
{
try
{
var oXMLHttp = new ActiveXObject(versions[i]);
return oXMLHttp;
}
catch(e){}
}
throw new Error("No XMLhttp");
}
</SCRIPT>
<P>
<TABLE id=Table1 cellPadding=3 width=300 bgColor=#efefef>
<TBODY>
<TR>
<TD align=middle><B>사원 정보 조회</B> </TD></TR></TBODY></TABLE>
<TABLE id=Table2 cellPadding=3 width=300>
<TBODY>
<TR>
<TD align=middle>사번</TD>
<TD><INPUT id=EmpID onchange=GetInfo(); name=EmpID></TD></TR>
<TR bgColor=silver height=1>
</TR>
<TR>
<TD align=middle width=80>이름</TD>
<TD width=200><INPUT id=LastName name=LastName></TD></TR>
<TR>
<TD align=middle>성</TD>
<TD><INPUT id=FirstName name=FirstName></TD></TR>
<TR>
<TD align=middle>직급</TD>
<TD><INPUT id=Title name=Title></TD></TR>
<TR>
<TD align=middle>생일</TD>
<TD><INPUT id=BirthDate name=BirthDate></TD></TR>
<TR>
<TD align=middle>도시</TD>
<TD><INPUT id=City name=City></TD></TR></TBODY></TABLE>
defaultJSON.htm
<SCRIPT language=javascript type=text/javascript>
var xmlHttp;
function GetInfo()
{
var empID = document.getElementById("EmpID").value;
var url = "processJSON.aspx?EmpID=" + empID;
xmlHttp = GetXMLHttp();
xmlHttp.open("GET", url, true);
xmlHttp.onreadystatechange = callback;
xmlHttp.send(null);
}
function callback()
{
if(xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
parseAndDisplay(xmlHttp.responseText);
}
else
{
var label = document.getElementById("errorText");
label.style.color = "red";
label.innerHTML = "에러 발생 : " + xmlHttp.statusText;
}
}
}
function parseAndDisplay( data )
{
eval("var emp = " + data);
document.getElementById("LastName").value = emp.LastName;
document.getElementById("FirstName").value = emp.FirstName;
document.getElementById("Title").value = emp.Title;
document.getElementById("BirthDate").value = emp.BirthDate;
document.getElementById("City").value = emp.City;
}
function GetXMLHttp()
{
//Firefox를 위한 코드
if(window.XMLHttpRequest) return new XMLHttpRequest(); // IE 6이하를 위한 코드
var versions = [
"MSXML2.XMLHttp.5.0",
"MSXML2.XMLHttp.4.0",
"MSXML2.XMLHttp.3.0",
"MSXML2.XMLHttp",
"Microsoft.XMLHttp"
];
for(var i = 0; i < versions.length; i++)
{
try
{
var oXMLHttp = new ActiveXObject(versions[i]);
return oXMLHttp;
}
catch(e){}
}
throw new Error("No XMLhttp");
}
</SCRIPT>
<P>
<TABLE id=Table1 cellPadding=3 width=300 bgColor=#efefef>
<TBODY>
<TR>
<TD align=middle><B>사원 정보 조회</B> </TD></TR></TBODY></TABLE>
<TABLE id=Table2 cellPadding=3 width=300>
<TBODY>
<TR>
<TD align=middle>사번</TD>
<TD><INPUT id=EmpID onchange=GetInfo(); name=EmpID></TD></TR>
<TR bgColor=silver height=1>
</TR>
<TR>
<TD align=middle width=80>이름</TD>
<TD width=200><INPUT id=LastName name=LastName></TD></TR>
<TR>
<TD align=middle>성</TD>
<TD><INPUT id=FirstName name=FirstName></TD></TR>
<TR>
<TD align=middle>직급</TD>
<TD><INPUT id=Title name=Title></TD></TR>
<TR>
<TD align=middle>생일</TD>
<TD><INPUT id=BirthDate name=BirthDate></TD></TR>
<TR>
<TD align=middle>도시</TD>
<TD><INPUT id=City name=City></TD></TR></TBODY></TABLE>
DB뭐기시 하기 귀찮아서 그냥 값을 뿌려서 결과를 봄 -_-;;
processJSON.aspx
Response.Write("{");
Response.Write(string.Format("LastName : \"세스\","));
Response.Write(string.Format("FirstName : \"람\","));
Response.Write(string.Format("Title : \"이집트왕자\","));
Response.Write(string.Format("BirthDate : \"BC 1318\","));
Response.Write(string.Format("City : \"이집트\""));
Response.Write("}");
Response.Write("{");
Response.Write(string.Format("LastName : \"세스\","));
Response.Write(string.Format("FirstName : \"람\","));
Response.Write(string.Format("Title : \"이집트왕자\","));
Response.Write(string.Format("BirthDate : \"BC 1318\","));
Response.Write(string.Format("City : \"이집트\""));
Response.Write("}");
모바일만 하다가 웹을 하니 참 신기한게 많다는..
반응형
'Dev > Ajax' 카테고리의 다른 글
[Ajax] UpdateProgress 컨트롤 맛보기 (0) | 2008.09.18 |
---|---|
[Ajax] JSON 배열 맛보기 (0) | 2008.09.11 |