CORS 从 pastebin 读取文本文件

CORS to read a text file from a pastebin

我希望使用 CORS 从 pastebin 加载代码片段,然后在浏览器中处理它们。

一些正在进行的代码在这里: http://www.boisvert.me.uk/opendata/sparql_aq+.html

代码被高亮显示,并且有运行它等的选项

我想提供一个简单的服务,用户可以在任何地方保存文本 public,然后查询:

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

例如URL是:

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

但是当使用 CORS 时,存储库 returns 是一个空文件。 CORS 是否被某些系统阻止(例如 pastebin.com?)或者我做错了什么?

我附上了来自 firefox 调试器的图像,显​​示了 CORS 返回的空白响应,除非我没记错,如果有帮助,GET headers.

最后,我的 CORS 代码:

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

要使其在客户端运行,您可以使用像 cors.io 这样的 CORS 代理,或者您可以编写自己的代理。

在使用 cors.io 的情况下,您可以像这样在服务的 url 前面添加。

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

购买 Pastebin Pro 帐户后,您的帐户粘贴将自动通过 CORS 公开检索(使用原始 link)

截至 2022 年 2 月 17 日,这似乎运行良好:https://allorigins.win/ 它是避免 CORS 问题的代理。使用此 link 时请自行 due-diligence。我与那个网站没有关系。