Search

4/10/2012

cors

if an ajax request is sent to a cross-domain, it won't carry cookie information
cross-domain example: http://jsfiddle.net/chunghe/nfvtd/10/ 
same-domain example: http://chunghe.herokuapp.com/cors.htm

function createCORSRequest(method, url) {
    var xhr = new XMLHttpRequest();
    if ("withCredentials" in xhr) {
        xhr.open(method, url, true);
    } else if (typeof XDomainRequest != "undefined") {
        xhr = new XDomainRequest();
        xhr.open(method, url);
    } else {
        xhr = null;
    }
    return xhr;
}

$('#go').click(function () {
    var request = createCORSRequest("get", "http://chunghe.herokuapp.com/cors.php");
    if (request) {
        request.onreadystatechange = function (data) {
            if (request.readyState == 4 && request.status == 200) {
              console.log(request.responseText);
            }                
        };
        request.send();
    }       
})
​

沒有留言: