CORS从引擎收录读取文本文件

问题描述 投票:1回答:2

我希望利用CORS从引擎收录加载代码片段,然后在浏览器中处理它们。

在建的一些代码是在这里: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头。

最后,我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();
}
javascript cors pastebin
2个回答
4
投票

为了使从客户端工作,你可以使用这样一个cors.io CORS代理或者你可以写你自己的。

在使用cors.io,你可以在前面加上这样的服务的URL的情况下。

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


1
投票

你买了引擎收录临帐户后,您的帐户膏会被自动地通过CORS公开检索(使用原始链接)

© www.soinside.com 2019 - 2024. All rights reserved.