javascriptcorspastebin

CORS to read a text file from a pastebin


I am hoping to use CORS to load code snippets from a pastebin, then process them in a browser.

Some code in progress is here: http://www.boisvert.me.uk/opendata/sparql_aq+.html

The code is highlighted and there are options to run it etc.

I'd like to provide a simple service, where a user saves the text anywhere public, then queries:

http://www.boisvert.me.uk/opendata/sparql_aq+.html?sparqlURL=whatever-url

for example, the URL is:

http://pastebin.com/raw.php?i=grUU9zwE

http://www.boisvert.me.uk/opendata/sparql_aq+.html?sparqlURL=http%3A%2F%2Fpastebin.com%2Fraw.php%3Fi%3DgrUU9zwE

But when using CORS, the repository returns an empty file. Is CORS blocked by some systems (e.g. by pastebin.com?) or what am I doing wrong?

I attach images from the firefox debugger, showing, unless I'm missing the point, the blank response returned by CORS, and in case that helps, the GET headers.

Firefox network tool - request OK, but result appears blank The same problem, different sceenshot

Finally, my CORS code:

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

Solution

  • To make it work from the client side you could use a CORS proxy like cors.io or you could write your own.

    In the case of using cors.io you could prepend the url of the service like this.

    https://cors.io/?http://pastebin.com/raw.php?i=grUU9zwE