JSFiddle - React, Tailwind, and code Playground

by somekittens

HTML

<?php
header("Access-Control-Allow-Origin: *");
echo "hey!";
?>

JavaScript

function submitForm(evt) {
    var request = createCORSRequest("GET", "test.php");
    if(request) {
        request.onload = function(){
            console.log(request.responseText);
        };
        request.send();
    } else {
        console.log("Yikes, something went wrong!");
    }        
    document.getElementById("message").innerHTML = "It worked!";
    evt.preventDefault();
    return false;
}

//Used with thanks to http://www.nczonline.net/blog/2010/05/25/cross-domain-ajax-with-cross-origin-resource-sharing/
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;
}