1

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

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

代码突出显示,并且有运行它的选项等。

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

http://www.boisvert.me.uk/opendata/sparql_aq+.html?sparqlURL=whatever-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 时,存储库会返回一个空文件。CORS是否被某些系统(例如pastebin.com?)阻止或者我做错了什么?

我附上了来自 firefox 调试器的图像,除非我错过了重点,否则我会显示 CORS 返回的空白响应,如果有帮助,还会显示 GET 标头。

Firefox 网络工具 - 请求确定,但结果显示为空白 同样的问题,不同的场景

最后,我的 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();
}
4

3 回答 3

3

为了让它在客户端工作,你可以使用像cors.io这样的 CORS 代理,或者你可以自己编写。

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

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

于 2018-06-26T02:42:21.947 回答
1

与 2022 年 2 月 17 日一样,这似乎运作良好:https ://allorigins.win/它是避免 CORS 问题的代理。使用此链接时,请自行进行尽职调查。我与那个网站没有任何联系。

于 2022-02-17T08:37:47.397 回答
0

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

于 2019-02-06T09:44:53.307 回答