【问题标题】:Querying Sesame triplestore using JavaScript使用 JavaScript 查询 Sesame Triplestore
【发布时间】: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


【解决方案1】:

具体错误是 (AFAIK) Chrome 特有的,并且与 Chrome 无法将 application/sparql-results+json 识别为与脚本兼容的媒体类型这一事实有关。要解决此问题,请在请求的 Accept 参数中将 mediatype 替换为 application/json

更一般地说,我应该指出,您在这里所做的与 CORS 无关。 CORS 是关于向每个请求/响应添加一组 HTTP 标头,以允许来自浏览器的跨域脚本调用。在这里,您使用的是 JSONP 回调,这是一种不同的(较旧,安全性稍差)机制来实现相同的目的。

FWIW Sesame Server 目前尚不支持 CORS 标头,尽管它在 ToDo 列表中:https://openrdf.atlassian.net/browse/SES-1757

【讨论】:

  • 我的错!我不小心关闭了服务器。之前的错误仍然存​​在。我正在相应地改变问题。
  • 感谢您的详细回复,Jeen。我花了一整天的时间试图解决这个问题,但没有解决方案。你认为这值得一试吗? arachnolingua.wordpress.com/tag/cors-filter我有点担心摆弄服务器的配置文件。
  • @kurious 绝对值得一试。
  • 我正在尝试实现它,但是当我尝试将 web.xml 文件保存在 C:\Program Files (x86)\Apache Software Foundation\Tomcat 7.0\webapps\ 中时收到拒绝访问消息openrdf-芝麻\WEB-INF。没有以管理员身份运行它的选项。关于如何编辑文件的任何建议?另外,我没有看到 .war 文件。我没有更改工作台 web.xml 文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-14
  • 1970-01-01
  • 1970-01-01
  • 2023-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多