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

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

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

게시글 목록

번호 제목
17523
17516
17515
17514
17498
17493
17490
17487
17485
17481
17478
17477
17474
17473
17472
17470
17469
17463
17462
17461
17460
17458
17457
17454
17453
17448
17447
17446
17445
17444