【发布时间】:2015-04-22 00:33:14
【问题描述】:
我正在尝试使用 ajax 从 Sesame Triplestore 检索数据。这可能是 CORS 问题,我正在尝试使用 CORS 过滤器解决它。我的假设是正确的还是我需要更改代码中的某些内容?
$(document).ready(function() {
$.ajax({
url: 'http://localhost:8080/openrdf-sesame/repositories/Test12',
dataType: 'jsonp',
data: {
queryLn: 'SPARQL',
query: "SELECT * WHERE { ?s ?p ?o }",
limit: 100,
infer: 'true',
Accept: 'application/sparql-results+json'
},
success: displayData,
error: displayError
});
});
function displayError(xhr, textStatus, errorThrown) {
alert(textStatus);
alert(errorThrown);
}
function displayData(data) {
var header = $('#result thead').append('<tr/>');
$.each(data.head.vars, function(key,value) {
header.append("<th>" + value + "</th>");
});
$.each(data.results.bindings, function(index, bs) {
var row = $('<tr/>');
$.each(data.head.vars, function(key, varname) {
row.append("<td>" + bs[varname].value + "</td>");
});
$("#result tbody").after(row);
});
}
我在 chrome 控制台中收到以下错误:
Resource interpreted as Script but transferred with MIME type application/sparql-results+json: "http://localhost:8080/openrdf-sesame/repositories/Test12?callback=jQuery213…=100&infer=true&Accept=application%2Fsparql-results%2Bjson&_=1429660808937". jquery-2.1.3.min.js:4
send jquery-2.1.3.min.js:4
n.extend.ajax jquery-2.1.3.min.js:4
(anonymous function) index_wip3.html:10
j jquery-2.1.3.min.js:2
k.fireWith jquery-2.1.3.min.js:2
n.extend.ready jquery-2.1.3.min.js:2
I jquery-2.1.3.min.js:2
Uncaught SyntaxError: Unexpected token : Test12:2
如果我将 application/sparql-results+json 替换为 application/json,则错误保持不变。
如果我将 dataType: 更改为“json”而不是“jsonp”,则错误变为:
XMLHttpRequest cannot load http://localhost:8080/openrdf-sesame/repositories/Test12?queryLn=SPARQL&que…HERE+%7B+%3Fs+%3Fp+%3Fo+%7D&limit=100&infer=true&Accept=application%2Fjson. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
【问题讨论】:
-
感谢您链接问题。我将交叉链接到上一个问题。
-
你说“如果我替换......,错误变为:”但在引用的块中我实际上没有看到错误消息。
标签: javascript ajax cors sesame