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

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

· 2년 전 · 669 · 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에 불법사이트 만들던 조선족이 등장했다면서요?

게시글 목록

번호 제목
18088
18080
18078
18070
18061
18037
18036
18033
18022
18021
18020
18018
18004
17999
17995
17987
17982
17981
17980
17970
17965
17964
17963
17962
17960
17956
17953
17949
17930
17928