My goal is to utilize CORS for fetching code snippets from a pastebin and then executing them in a web browser.
Here is some work-in-progress code: http://www.example.com/code-snippets.html
The code is syntax highlighted with options to run it, etc.
I aim to offer a straightforward service where users save text in a public location and then query:
http://www.example.com/code-snippets.html?queryURL=specific-url
For instance, the URL could be:
However, when using CORS, the repository returns an empty file. Is CORS possibly blocked by certain systems (e.g., pastebindemo.com)? Or am I making an error somewhere?
I have attached images from the Firefox debugger that show what seems like a blank response from CORS, along with the GET headers which may provide insight.
Below is my CORS code snippet:
function CORSRequest(url) {
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
// Check if the XMLHttpRequest object has a "withCredentials" property.
// "withCredentials" only exists on XMLHTTPRequest2 objects.
xhr.open("GET", url, true);
} else if (typeof XDomainRequest != "undefined") {
// Otherwise, check if XDomainRequest.
// XDomainRequest only exists in IE, and is IE's way of making CORS requests.
xhr = new XDomainRequest();
xhr.open("GET", url);
} else {
// Otherwise, CORS is not supported by the browser.
throw new Error('CORS not supported');
}
if (xhr) {
xhr.onload = function() {
// process the response.
document.getElementById("sparql").value = xhr.responseText;
};
xhr.onerror = function() {
alert('Not loading.');
};
}
xhr.send();
}