JSFiddle - React, Tailwind, and code Playground

by chunghe

HTML

<input type="button" id="go" value="make a cors request">

JavaScript

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();
    }       
})