테스트 사이트 - 개발 중인 베타 버전입니다

jQuery없이 네티브자바스크립트로 서버에 요청보내기 6- CORS

· 2년 전 · 670 · 1

CORS, or Cross Origin Resource Sharing (sending cross-domain ajax requests) 에 대해서는 개발분들은 잘 아실거라 봅니다.

먼저 jQuery코드를 본다면

[code]

$.ajax('http://someotherdomain.com', {

    method: 'POST',

    contentType: 'text/plain',

    data: 'sometext',

    beforeSend: function(xmlHttpRequest) {

        xmlHttpRequest.withCredentials = true;

    }

});

[/code]

위의 코드처럼 jQuery는 withCredentials 값을 설정하여 요청헤더에 첨부하여 요청을 보냅니다.

그러니 이기능은 IE8,9에서는 작동하지 못합니다.

그것은 IE8,9는 withCredentials가 아니라 XDomainRequest 를 사용하기때문입니다.

네티브자바스크립트로 다음과 모든 웹브라우저에서 사용가능한 코드는 다음과 같습니다.

[code]

// For cross-origin requests, some simple logic

// to determine if XDomainReqeust is needed.

if (new XMLHttpRequest().withCredentials === undefined) {

    var xdr = new XDomainRequest();

    xdr.open('POST', 'http://someotherdomain.com');

    xdr.send('sometext');

}

[/code]

 

 

댓글 작성

댓글을 작성하시려면 로그인이 필요합니다.

로그인하기

댓글 1개

2년 전
sir에 불법사이트 만들던 조선족이 등장했다면서요?

게시글 목록

번호 제목
18200
18195
18193
18181
18179
18173
18170
18164
18158
18155
18152
18151
18150
18140
18139
18138
18131
18130
18120
18119
18118
18117
18116
18111
18110
18108
18107
18106
18100
18090