한글이 깨지는 것은 다음 두가지 방법으로 해결한다.
1. 첫째 ajax로 호출하는 asp 소스에 CharSet을 지정한 경우
<% >
' ## 파일명 : test.asp
Response.CharSet = "euc-kr"
%>
<%'## 아래 meta 태그 charset 지정과는 아무 상관없음, 없어도 됨%>
~~~~
~~~~ 내용에 한글이 있어요. ㅋㅋ ~~
<% >
' ## 파일명 : call.asp
%>
~~~
~~~~
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}
function startRequest() {
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET", "/~/test.asp", true);
xmlHttp.send(null);
}
function handleStateChange() {
if(xmlHttp.readyState == 4) {
if(xmlHttp.status == 200) {
document.getElementById("idView").innerHTML = xmlHttp.responseText;
}
}
}
</script>
~~~
~~~
소스 구성이 위와 비슷할 경우 GET, POST 방식과 상관없이 responseText로 처릴 해도 정상적으로 한글이 보여집니다.
그리고 POST방식으로 처리할때 xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=euc-kr"); 와 같은 구문이 추가되는데 여기에서의 charset과도 상관없이 진행됩니다.
이때 호출되는 파일에 반드시 CharSet 이 지정되어 있어야 합니다.
2. 둘째 ajax로 호출하는 asp 소스가 CharSet 지정없는 html인 경우
<% >
' ## 파일명 : test.asp
' ###Response.CharSet = "euc-kr" ' ### 주석처리됨
%>
~~~~
~~~~ 내용에 한글이 있어요. ㅋㅋ ~~
<% >
' ## 파일명 : call.asp
%>
~~~
~~~~
<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest() {
if (window.ActiveXObject) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
}
}
function startRequest() {
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET", "/~/test.asp", true);
xmlHttp.send(null);
}
function handleStateChange() {
if(xmlHttp.readyState == 4) {
if(xmlHttp.status == 200) {
document.getElementById("idView").innerHTML = BinDecode(xmlHttp.responseBody);
}
}
}
</script>
<script language="vbscript">
'-----------------------------------------
' BinDecode (바이너리 -> 아스키 변환)
'-----------------------------------------
Public Function BinDecode(byVal binData)
Dim i, byteChr, strV
For i = 1 to LenB(binData)
byteChr = AscB(MidB(binData,i,2))
If byteChr > 127 Then
i = i + 1
strV = strV & Chr("&H" & Hex(byteChr) & Hex(AscB(MidB(binData,i,2))))
Else
strV = strV & Chr(byteChr)
End if
Next
BinDecode = strV
End Function
</script>
~~~
~~~
CharSet 지정없는 이번의 경우엔 responseText 로 읽어오면 html내 한글이 모두 깨집니다. 그래서 responseBody 로 바이너리 타입으로 읽어 들이고 바이너리를 아스키로 변환하는 펑션에 입힙니다.
검색해 보면 어렵지 않게 찾을 수 있죠. ^^
결론을 내면 개발할때 CharSet 를 지정을 해줄 수 있는지에 따라 방법적인 것을 달리해서 처리하면 될거라 생각됩니다. 자신이 관리하지 않는 외부 소스를 읽어 들일려면 두번째 방법으로 해야 되겠죠.
본 글에 문제가 있으면 바로 연락주세요. gatemail@hanmail.net
댓글 없음:
댓글 쓰기