ASP 페이지가 EUC-KR로 인코딩이 되어 있을때, jquery ajax로 값을 넘기게 되면 기본으로 UTF-8로 인코딩이 되어 넘어가게 된다.

이렇게 되면 값이 깨지는 현상이 발생한다.

이 경우도 두가지로 분류를 할수가 있는데

1. 텍스트만 넘기는 경우

2. 파일을 첨부해서 넘기는 경우

가 처리 방식이 다르다.


1. 텍스트만 AJAX를 이용해서 넘기는 경우

$.ajax({

type: "POST",

dataType: "json",

url: "countdown_main_edit_ok.asp",

cache:false,

async: false,

data: encodeURI( $("#frm").serialize() )

});


이런식으로 값을 encodeURI로 인코딩을 해서 넘겨주고, 이 값을 받는 곳에서는 UTF8을 디코딩해서 처리한다.

UTF-8로 디코딩을 하기 위해선 별도의 함수가 필요한데. 다음 함수를 사용하면 된다.


<%

Public  Function URLDecodeUTF8(byVal strUrl)

Dim strBase,ub

Dim UtfB

Dim UtfB1, UtfB2, UtfB3

Dim moveSeq, strText

ub  = 0


For moveSeq = 1 To Len(strUrl)

strBase = Mid(strUrl, moveSeq, 1)

Select Case strBase

Case "+"

strText = strText & " "

Case "%"

ub  = Mid(strUrl, moveSeq + 1, 2)

UtfB = CInt("&H" & ub)

If UtfB<128 Then

moveSeq = moveSeq + 2

strText = strText & ChrW(UtfB)

Else

UtfB1=(UtfB And &H0F) * &H1000

UtfB2=(CInt("&H" & Mid(strUrl, moveSeq + 4, 2)) And &H3F) * &H40

UtfB3=CInt("&H" & Mid(strUrl, moveSeq + 7, 2)) And &H3F

strText = strText & ChrW(UtfB1 Or UtfB2 Or UtfB3)

moveSeq = moveSeq + 8

End If

Case Else

strText = strText & strBase

End Select

Next

URLDecodeUTF8 = strText

End Function

%>


사용방법은 다음과 같이 하면 된다.

a = URLDecodeUTF8(Request("a"))


2. 파일과 텍스트를 함께 AJAX를 이용해서 넘기는 경우

AJAX를 이용해서 파일과 텍스트를 함께 전달하는 경우는 2가지 정도 방법이 있는데, 브라우저 호환성 때문에 

jquery.form.min.js을 이용한다. 


폼 처리는 다음과 같이 일반적인 폼 submit 처럼 하면 되고

$('#frm').prop("action","insert_ok.asp").submit();


다음과 같이 ajaxForm을 이용해서 처리해준다.

$('#frm').ajaxForm({

       //보내기전 validation check가 필요할경우

beforeSubmit: function (data, frm, opt) {

},


//submit이후의 처리

success: function(responseText, statusText){

// alert("정상");

},


//ajax error


error: function(){


alert("에러발생!!");


}                               

  });


이를 받는 곳이 중요한데 받는 곳의 파일 인코딩은 EUC-KR 이므로 ANSI 이어야 하고,

DEXTUPLOAD의 경우에 uploadform.CodePage 가 있는데 이게 브라우저 버전별로 다르게 처리를 해야한다.

브라우저 버전별로 인코딩이 다르게 반응을 하기 때문이다.


Dim IE, strUserAgent

strUserAgent = UCase(cstr(request.ServerVariables("HTTP_USER_AGENT")))

'인터넷 익스플로러

If InStr(strUserAgent, "MSIE 6.0") Then

    IE = 6

ElseIf InStr(strUserAgent, "MSIE 6.1") Then

    IE = 6

ElseIf InStr(strUserAgent, "MSIE 6.2") Then

    IE = 7

ElseIf InStr(strUserAgent, "MSIE 7") Then

    IE = 7

ElseIf InStr(strUserAgent, "MSIE 8") Then

IE = 8

ElseIf InStr(strUserAgent, "MSIE 9") Then

IE = 9

ElseIf InStr(strUserAgent, "MSIE 10") Then

IE = 10

ElseIf InStr(strUserAgent, "MSIE 11") Then

IE = 11

Else '기타..크롬등

IE = 12

End If


If IE >= 10 Then '최신버전에서는 65001 을 해야 안깨지고, IE 구버전에서는 65001을 빼야 안깨진다.

uploadform.CodePage = 65001

End If



//