您应该检查的第一件事是,当您取消引用端点时,您请求的是 RDF 格式的内容,而不是 web 浏览器默认指定的 text/html 等内容,并且端点没有基于用户代理等为您提供某些输出。例如,如果您访问http://dbpedia.org/sparql上的 DBpedia 端点,您将获得一个 HTML 查询编辑器。但是,如果您请求该页面时将 Accept 标头设置为“application/rdf+xml”,您将获得服务描述。在不知道您遇到问题的端点的情况下,我们真的无法提供更多帮助。这应该工作,但某些端点不这样做,这不是我们可以调试的真正技术问题,特别是如果您不告诉我们您遇到问题的端点。这是使用 curl 的样子:
$ curl -H "Accept: application/rdf+xml" http://dbpedia.org/sparql
<?xml version="1.0" encoding="utf-8" ?>
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:sd="http://www.w3.org/ns/sparql-service-description#" >
<rdf:Description rdf:about="http://dbpedia.org/sparql">
<rdf:type rdf:resource="http://www.w3.org/ns/sparql-service-description#Service" />
<sd:endpoint rdf:resource="http://dbpedia.org/sparql" />
<sd:feature rdf:resource="http://www.w3.org/ns/sparql-service-description#UnionDefaultGraph" />
<sd:feature rdf:resource="http://www.w3.org/ns/sparql-service-description#DereferencesURIs" />
<sd:resultFormat rdf:resource="http://www.w3.org/ns/formats/SPARQL_Results_JSON" />
<sd:resultFormat rdf:resource="http://www.w3.org/ns/formats/SPARQL_Results_XML" />
<sd:resultFormat rdf:resource="http://www.w3.org/ns/formats/Turtle" />
<sd:resultFormat rdf:resource="http://www.w3.org/ns/formats/N-Triples" />
<sd:resultFormat rdf:resource="http://www.w3.org/ns/formats/N3" />
<sd:resultFormat rdf:resource="http://www.w3.org/ns/formats/RDF_XML" />
<sd:resultFormat rdf:resource="http://www.w3.org/ns/formats/SPARQL_Results_CSV" />
<sd:resultFormat rdf:resource="http://www.w3.org/ns/formats/RDFa" />
<sd:supportedLanguage rdf:resource="http://www.w3.org/ns/sparql-service-description#SPARQL10Query" />
<sd:url rdf:resource="http://dbpedia.org/sparql" />
</rdf:Description>
这是一个使用 d3 的 XmlHttpRequest 功能的实时版本。(我知道你可以在没有库的情况下做到这一点,但我最近一直在使用很多 d3。)
/**
* Make a GET request to dbpedia.org/sparql
* and show the response in a PRE element.
*/
d3.xhr("http://dbpedia.org/sparql")
.get(function(error,data) {
console.log(error);
console.log(data);
d3.select("body")
.append("pre")
.text(data.response);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script>