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

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

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개

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

게시글 목록

번호 제목
17591
17590
17589
17588
17587
17584
17583
17582
17581
17579
17577
17575
17574
17573
17572
17569
17564
17560
17559
17555
17554
17553
17552
17549
17548
17547
17542
17533
17531
17524