最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - CORS to read a text file from a pastebin - Stack Overflow

programmeradmin2浏览0评论

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: +.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:

+.html?sparqlURL=whatever-url

for example, the URL is:

.php?i=grUU9zwE

+.html?sparqlURL=http%3A%2F%2Fpastebin%2Fraw.php%3Fi%3DgrUU9zwE

But when using CORS, the repository returns an empty file. Is CORS blocked by some systems (e.g. by pastebin?) 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.

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

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./raw.php?i=grUU9zwE

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

But when using CORS, the repository returns an empty file. Is CORS blocked by some systems (e.g. by pastebin.?) 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.

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();
}
Share Improve this question asked Apr 25, 2015 at 22:06 boisvertboisvert 3,7392 gold badges29 silver badges55 bronze badges 1
  • 3 CORS has to be enabled server side, you cannot enable it from a client. Instead you can make an ajax request to some server side script on your server that would grab whatever url and return it – Patrick Evans Commented Apr 25, 2015 at 22:10
Add a ment  | 

3 Answers 3

Reset to default 3

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./raw.php?i=grUU9zwE

As on 17-Feb-2022 this seems to work well: https://allorigins.win/ It is a proxy to avoid CORS issues. Please do your own due-diligence when using this link. I have no connection with that website.

After you buy a Pastebin Pro account, your account pastes are automagically publicly retrievable through CORS (use the raw link)

发布评论

评论列表(0)

  1. 暂无评论