我希望使用 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
但是在使用 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();
}