일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- XML
- ASP.NET Error
- JavaScript
- Oracle 10g
- 스크립트릿
- ASP.NET 방명록
- JDK1.5
- SQL 명령어가 올바르게 종료되지 않았습니다.
- Ajax한글 처리문제
- ORA-00942
- DataList컨트롤
- 자동완성기능
- ASP.NET 회원가입
- 테이블또는 뷰가 존재하지 않습니다.
- 이벤트 처리
- ajax
- jsp
- prototype.js
- RowCount 폐이징
- 페이지 간 게시
- json
- prototype
- 자바스윙
- hyperlink 쿼리스트링 바인딩
- ASP.NET
- Ajax댓글
- Repeater
- 웹표준
- 우편번호검색
- 객체생성
- Today
- Total
IT 쟁이
2-1 XMLHttpRequest 객체 예제 본문
저는 jsp를 조금 밖에 공부안해봐서 그런지 이책의 예제는 잘이해가 안되더군요.그래서 .NET에서 돌아가게끔 몇가지 부분을 바꿔서 만들었습니다. 책에서는 httpRequest.js파일을 모두 참조하여 했었는데..아무래도
.NET에서는 이상하게 형식이 일치하지 않는다는 오류가 나서 .js파일 도 수정하였습니다.
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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">
<meta http-equiv="Content-Type" content="text/html; charset=euc-kr;" />
<title>제목 없음</title>
<script type="text/javascript">
function getXMLHttpRequest() {
if (window.ActiveXObject) {
try {
return new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try {
return new ActiveXObject("Microsoft.XMLHTTP");
} catch(e1) { return null; }
}
} else if (window.XMLHttpRequest) {
return new XMLHttpRequest();
} else {
return null;
}
}
var httpRequest = null;
function helloToServer()
{
var pageurl ="Default2.aspx?name="+ document.forms[0]['TextBox1'].value;
httpRequest = getXMLHttpRequest(); //크로스 브라우저 문제
httpRequest.open("POST", pageurl, true);
httpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
httpRequest.onreadystatechange = helloFromServer; //콜백함수 지정
httpRequest.send(null);
}
function helloFromServer()
{
if(httpRequest.readyState == 4)
{
if(httpRequest.status == 200)
{
alert(httpRequest.responseText);
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><input id="Button1" type="button" value="열림" onclick="helloToServer();" />
</form>
</body>
</html>
Default2.aspx 생성하고 다음과 같이 코딩하였다.
protected void Page_Load(object sender, EventArgs e)
{
string id = Request.QueryString["name"].ToString();
if (Request.QueryString["name"] != null)
{
Response.Write(id);
}
Response.End();
}
웹페이지를 생성하면 다음과 같이 된다.
한글처리문제를 해주지 않기때문에 -_-이런일이 발생합니다. 이럴땐 web.config 다음과 같은 설정을 추가합니다.
<globalization requestEncoding="ks_c_5601-1987" responseEncoding="ks_c_5601-1987"/>
또는
<globalization requestEncoding="euc-kr" responseEncoding="euc-kr"/>
정상적으로 출력되는것을 알 수 있다.
다음과 같은 메시지가 발생할 경우는 -_- 서버쪽코드 에서 Response.End()메서드가 빠져있는지 꼭 확인을 해야된다.
-ㅇ- 오늘도 오랜만에 Ajax코딩을하다보니 몇시간은 해맺던거 같다. 조심하길!!!!!