Microsoft MVP성태의 닷넷 이야기
.NET Framework: 237. WCF AJAX 서비스와 JavaScript 간의 DateTime 연동 [링크 복사], [링크+제목 복사],
조회: 22206
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
(연관된 글이 3개 있습니다.)

WCF AJAX 서비스와 JavaScript 간의 DateTime 연동

일단, JSON 개체를 AJAX 서비스에 전달하는 방법은 다음의 글을 참조하시고.

Using complex types to make calling services less… complex
; http://encosia.com/using-complex-types-to-make-calling-services-less-complex/

여기서는, 자바스크립트에서 Date 타입을 전달/반환받는 방법을 살펴보겠습니다.

우선, AJAX 서비스 측에서 DateTime을 반환해주는 값을 자바스크립트에서 사용하는 것을 다뤄보기 위해 다음과 같이 간단한 예제 코드를 만들어 보겠습니다.

===== 서버 측 AJAX 서비스 =====
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class AjaxService
{
    [OperationContract]
    [WebGet(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    public DateTime DoWork()
    {
        HttpContext.Current.Response.Expires = 0;

        return DateTime.UtcNow;
    }
}

===== 클라이언트 측 자바스크립트 =====
<script type="text/javascript">

    function ajaxJson(serviceUrl, httpMethod, method, data, callback, error) {
        var url = serviceUrl + "/" + method;
        $.ajax({
            url: url,
            data: data,
            type: httpMethod,
            processData: true,
            contentType: "application/json",
            timeout: 1000 * 10,
            dataType: "json",
            success: callback,
            error: error
        });
    }

    function DoWorkResponse(result) {
        var dateNow = result.d;

        alert("result: " + dateNow);
    }

    function DoWorkError() {
    }

    ajaxJson(ajaxRootUrl, "GET", "DoWork", "", DoWorkResponse, DoWorkError);
        
</script>

이렇게 실행해서 반환받은 값은 다음과 같은 JSON 형태로 직렬화 되어 있습니다.

{"d":"\/Date(1313673383715)\/"}

자바 스크립트에서는 이렇게 구한 'd' 속성값이 '문자열'로 역직렬화된다는 것을 차치하고라도, Date(...) 내부에 있는 숫자의 값이 궁금합니다. 이에 대해서는 다음의 글에서 설명되고 있는데요.

DateTime to javascript date
; http://stackoverflow.com/questions/2404247/datetime-to-javascript-date

C#의 DateTime 기준으로, "1970년 1월 1일 날짜를 뺀 총 ms(Millisecond) 값"인데 C# 코드에서 직접 이 값을 구하려면 위의 글에 달린 댓글대로 다음과 같이 처리할 수 있습니다.

DateTime now = DateTime.UtcNow;
System.Diagnostics.Debug.WriteLine(now.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds); // 소수점 자리가 생기지만!

이쯤 되면 위의 계산에서 사용된 "1970년 1월 1일"이라는 특별한 날짜가 의아스러울 텐데요. 역시 다음의 글을 읽어보시면 도움이 되실 것입니다.

time
; http://jangyeoh.mzin.net/V0/lecture/language/c/time.txt

(즉, "Epoch time == 1970년 1월 1일"인데 현재 C언어에서는 long 형으로 초 단위 시간기록을 하고 있어서 2038년 이상의 시간 표시를 할 수 없다고 합니다.)

Epoch 기준 시간 자체를 C# DateTime의 Ticks로 바꾸면 어떤 값이 나올까요?

new DateTime(1970, 1, 1).Ticks
    ==> 621355968000000000

이 숫자는 자바와 C#의 시간 연동을 해본 분이라면 낯이 익을 텐데요. 실제로 자바의 System.currentTimeMillis()가 반환하는 값은 다음의 C# 코드와 동일합니다.

System.currentTimeMillis() == (System.DateTime.UtcNow.Ticks - 621355968000000000) / TimeSpan.TicksPerMillisecond;

정리해 보면, AJAX 서비스에서는 DateTime에 대해서 직렬화를 Epoch 기준의 시간으로 변환한 ms(Millisecond) 값을 "/Date(...)/"로 감싼 문자열로 반환하고 있습니다.

그럼, 이 값을 자바스크립트의 Date 개체로 바꾸려면 어떻게 해야 할까요? 다행히 Date 개체는 Epoch 기준의 ms 값을 받는 생성자를 가지고 있기 때문에 다음과 같은 eval 구문으로 "/" 문자를 없애고 직접 Date 개체를 생성할 수 있습니다.

function DoWorkResponse(result) {
    var now = eval("new " + result.d.replace(/\//g, '') + ";"); // 예) eval("new Date(1313673383715);");
    alert(now);
}




다음은, 자바스크립트에서 AJAX 서비스로 date 타입을 전달하는 것을 알아보겠습니다.

'직관적인 기준'으로 보면 다음과 같이 구현해 줄 수 있을 것입니다.

===== 서버 측 AJAX 서비스 =====
[WebGet(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public void SendTime(DateTime now)
{
    HttpContext.Current.Response.Expires = 0;

    System.Diagnostics.Debug.WriteLine(now);
}

===== 클라이언트 측 자바스크립트 =====
var dateTimeNow = new Date();
ajaxJson(ajaxRootUrl, "GET", "SendTime", "now=" + dateTimeNow, SendTimeResponse, SendTimeError);

호출이 잘 될까요? 아쉽게도 오류가 발생하는데, 왜냐하면 다음과 같은 형식의 문자열로 직렬화되어 GET 메서드의 파라미터로 전달되기 때문입니다.

===== 요청 =====
GET http://localhost:26514/AjaxService.svc/SendTime?now=Fri%20Aug%2019%2000:02:11%20UTC+0900%202011 HTTP/1.1
Content-Type: application/json
X-Requested-With: XMLHttpRequest
Accept: application/json, text/javascript, */*
...[생략]...

===== 반환 =====
HTTP/1.1 500 Internal Server Error
Content-Length: 509
Content-Type: application/json; charset=utf-8
jsonerror: true
...[생략]...

{"ExceptionDetail":null,"ExceptionType":null,"Message":"The server was unable to process the request due to an internal error.  For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the  configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework 3.0 SDK documentation and inspect the server trace logs.","StackTrace":null}

"Fri%20Aug%2019%2000:02:11%20UTC+0900%202011"과 같은 값으로 직렬화되어 넘어가는 값을 AJAX 서비스는 정상적으로 DateTime 값으로 변환하지 못하고 예외를 발생합니다.

그럼 어떻게 전달해야 할까요? 맞습니다. 반환받을 때와 동일하게 "\/Date(1313673383715)\/"와 같은 식으로 전달해 주어야 합니다. 다행히 자바스크립트에서 epoch 기준의 시간으로 변환하는 것은 어렵지 않은데, 대신 JSON 포맷 및 UrlEncode와 같은 부가적인 추가 작업이 필요해서 다음과 같이 다소 번거로운 처리를 해주어야 합니다.

function ToEpochBasedTimeForGET(dateObj) {
    return "\"\\/Date(" + dateObj.getTime() + ")\\/\"";  // "\/Date(1313673383715)\/"  (인용 부호 포함해서!)
}

var dateTimeNow = new Date();
ajaxJson(ajaxRootUrl, "GET", "SendTime", "now=" + escape(ToEpochBasedTimeForGET(dateTimeNow)), SendTimeResponse, SendTimeError);

===== HTTP 요청 =====
GET http://localhost:26514/AjaxService.svc/SendTime?now=%22%5C/Date%281313682191870%29%5C/%22 HTTP/1.1
Content-Type: application/json
...[생략]...

물론, POST/PUT 방식이라면 GET 방식에서 필요했던 UrlEncode 과정을 생략해 줄 수 있는데, 대신 JSON 직렬화 자체에 변수명을 함께 전달해 주어야 합니다.

===== 서버 측 AJAX 서비스 =====
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public void SendTime2(DateTime now)
{
    HttpContext.Current.Response.Expires = 0;

    System.Diagnostics.Debug.WriteLine(now + ", Kind: " + now.Kind);
}

===== 클라이언트 측 자바스크립트 =====
function ToEpochBasedTime(paramName, dateObj) {
    return "{ \"" + paramName + "\": \"\\/Date(" + dateObj.getTime() + ")\\/\" }"; // { "now":  "\/Date(1313673383715)\/" }
}

var dateTimeNow = new Date();
ajaxJson(ajaxRootUrl, "POST", "SendTime2", ToEpochBasedTime("now", dateTimeNow), SendTimeResponse, SendTimeError);

===== HTTP 요청 =====
POST http://localhost:26514/AjaxService.svc/SendTime2 HTTP/1.1
Content-Type: application/json
Host: localhost:26514
Content-Length: 36
...[생략]...

{ "now": "\/Date(1313682191870)\/" }

이 정도면, 더 이상의 설명은 필요 없겠지요. ^^

첨부된 파일은 위의 코드를 포함한 예제 프로젝트입니다.




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 10/17/2021]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 



2016-10-08 11시54분
시간 정보 표현 방식 (Time Expressions)
; http://blog.naver.com/lovepeace/220830360898
정성태

... 16  17  18  19  20  [21]  22  23  24  25  26  27  28  29  30  ...
NoWriterDateCnt.TitleFile(s)
13125정성태9/14/20227710.NET Framework: 2048. C# 11 - 구조체 필드의 자동 초기화(auto-default structs)
13124정성태9/13/20227506.NET Framework: 2047. Golang, Python, C#에서의 CRC32 사용
13123정성태9/8/20227858.NET Framework: 2046. C# 11 - 멤버(속성/필드)에 지정할 수 있는 required 예약어 추가
13122정성태8/26/20227818.NET Framework: 2045. C# 11 - 메서드 매개 변수에 대한 nameof 지원
13121정성태8/23/20225668C/C++: 157. Golang - 구조체의 slice 필드를 Reflection을 이용해 변경하는 방법
13120정성태8/19/20227311Windows: 209. Windows NT Service에서 UI를 다루는 방법 [3]
13119정성태8/18/20226877.NET Framework: 2044. .NET Core/5+ 프로젝트에서 참조 DLL이 보관된 공통 디렉터리를 지정하는 방법
13118정성태8/18/20225661.NET Framework: 2043. WPF Color의 기본 색 영역은 (sRGB가 아닌) scRGB [2]
13117정성태8/17/20227883.NET Framework: 2042. C# 11 - 파일 범위 내에서 유효한 타입 정의 (File-local types)파일 다운로드1
13116정성태8/4/20228330.NET Framework: 2041. C# - Socket.Close 시 Socket.Receive 메서드에서 예외가 발생하는 문제파일 다운로드1
13115정성태8/3/20228714.NET Framework: 2040. C# - ValueTask와 Task의 성능 비교 [1]파일 다운로드1
13114정성태8/2/20228866.NET Framework: 2039. C# - Task와 비교해 본 ValueTask 사용법파일 다운로드1
13113정성태7/31/20228201.NET Framework: 2038. C# 11 - Span 타입에 대한 패턴 매칭 (Pattern matching on ReadOnlySpan<char>)
13112정성태7/30/20228566.NET Framework: 2037. C# 11 - 목록 패턴(List patterns) [1]파일 다운로드1
13111정성태7/29/20228314.NET Framework: 2036. C# 11 - IntPtr/UIntPtr과 nint/nuint의 통합파일 다운로드1
13110정성태7/27/20228381.NET Framework: 2035. C# 11 - 새로운 연산자 ">>>" (Unsigned Right Shift)파일 다운로드1
13109정성태7/27/20229783VS.NET IDE: 177. 비주얼 스튜디오 2022를 이용한 (소스 코드가 없는) 닷넷 모듈 디버깅 - "외부 원본(External Sources)" [1]
13108정성태7/26/20227725Linux: 53. container에 실행 중인 Golang 프로세스를 디버깅하는 방법 [1]
13107정성태7/25/20226820Linux: 52. Debian/Ubuntu 계열의 docker container에서 자주 설치하게 되는 명령어
13106정성태7/24/20226616오류 유형: 819. 닷넷 6 프로젝트의 "Conditional compilation symbols" 기본값 오류
13105정성태7/23/20227920.NET Framework: 2034. .NET Core/5+ 환경에서 (프로젝트가 아닌) C# 코드 파일을 입력으로 컴파일하는 방법 - 두 번째 이야기 [1]
13104정성태7/23/202210988Linux: 51. WSL - init에서 systemd로 전환하는 방법
13103정성태7/22/20227490오류 유형: 818. WSL - systemd-genie와 관련한 2가지(systemd-remount-fs.service, multipathd.socket) 에러
13102정성태7/19/20226952.NET Framework: 2033. .NET Core/5+에서는 구할 수 없는 HttpRuntime.AppDomainAppId
13101정성태7/15/202215923도서: 시작하세요! C# 10 프로그래밍
13100정성태7/15/20228341.NET Framework: 2032. C# 11 - shift 연산자 재정의에 대한 제약 완화 (Relaxing Shift Operator)
... 16  17  18  19  20  [21]  22  23  24  25  26  27  28  29  30  ...