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

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

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

게시글 목록

번호 제목
17927
17926
17922
17921
17915
17910
17907
17892
17888
17879
17878
17874
17873
17872
17871
17870
17869
17868
17866
17865
17864
17863
17862
17859
17856
17845
17835
17834
17826
17823