我试图通过ajax发送sparql查询,但是有错误。
首先,此代码可以正常工作。
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<style type="text/css">
img {
width: 100%;
}
</style>
</head>
<body></body>
<script type="text/javascript">
var url = "http://dbpedia.org/sparql";
var query = "\
PREFIX dbpedia2: <http://dbpedia.org/property/>\
PREFIX foaf: <http://xmlns.com/foaf/0.1/>\
SELECT DISTINCT *\
WHERE {\
?s dbpedia2:genus \"Apis\"@en; \
foaf:depiction ?o\
}";
var queryUrl = encodeURI( url+"?query="+query+"&format=json" );
$.ajax({
dataType: "jsonp",
url: queryUrl,
success: function( _data ) {
var vars = _data.head.vars;
var result = _data.results.bindings;
$('body').append('<table></table>');
var table = $('table');
$('table').append('<tr><th>?s</th><th>?o</th></tr>');
var str = "<tr>";
for(var i = 2; i < vars.length; i++)
{
str += '<th>'+vars[i].value+'</th>';
alert(str);
}
str += "</tr>";
table.append(str);
str = "";
for(var i = 0; i < result.length; i++)
{
str += "<tr>";
for(var j = 0; j < vars.length; j++)
{
if(result[i].hasOwnProperty(vars[j])) str += '<td>'+result[i][vars[j]]['value']+'</td>';
else str += '<td></td>';
}
str += "</tr>";
}
table.append(str);
// }
},
});
</script>
</html>
更改查询部分后,它不起作用。
var query = "\
PREFIX dbpedia2: <http://dbpedia.org/property/>\
PREFIX foaf: <http://xmlns.com/foaf/0.1/>\
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> \
SELECT ?s ?slabel ?o\
WHERE {\
?s dbpedia2:genus \"Apis\"@en;\
rdfs:label ?slabel\
foaf:depiction ?o\
}";
你能告诉我那个错误请求错误的原因吗? 谢谢!