查询使用JavaScript芝麻triplestore芝麻、JavaScript、triplestore

2023-09-10 21:59:20 作者:野性极端又美丽

我尝试从一个芝麻Triplestore用ajax的数据。这可能是一个CORS的问题,我想用CORS过滤器来解决这个问题。是我的假设是正确的或我需要改变的东西在code?

  $(文件)。就绪(函数(){
      $阿贾克斯({
                网址:HTTP://本地主机:8080 / openrdf-芝麻/库/ TEST12',
                数据类型:JSONP,
                数据: {
                    queryLn:SPARQL,
                    查询:SELECT *其中{S P 2 O?}?,
                    限制:100,
                    推断:真,
                    接受:应用程序/ SPARQL-结果+ JSON
                },
                成功:displayData,
                错误:displayError
        });
    });

    功能displayError(XHR,textStatus,errorThrown){
        警报(textStatus);
        警报(errorThrown);
    }

    函数displayData(数据){
        。VAR标题= $('#结果THEAD)追加('< TR />');
        $每个(data.head.vars,功能(键,值){
            header.append(百分位>中+值+&所述; /第i个);
        });


        $每个(data.results.bindings,函数(指数,BS){
        VAR行= $('< TR />');
        $每个(data.head.vars,功能(键,varname的){
            row.append(&其中; TD>中+ BS [varname的]。价值+&所述; / TD>中);
            });
        $(#结果TBODY)之后(行)。
        });
    }
 

我得到了镀铬控制台以下错误:

  PTED为脚本,但与MIME类型application / SPARQL-成果转移资源跨$ P $ + 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
发送的jQuery 2.1.3.min.js:4
n.extend.ajax jQuery的-2.1.3.min.js:4
(匿名函数)index_wip3.html:10
Ĵ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
我的jQuery 2.1.3.min.js:2
未捕获的SyntaxError:意外的标记:TEST12:2
 

如果我更换申请/ SPARQL-结果+ JSON与应用/ JSON,误差保持不变。

如果我更改了数据类型:以代替JSONPJSON,错误的更改:

  XMLHtt prequest无法加载http://localhost:8080/openrdf-sesame/repositories/Test12?queryLn=SPARQL&que…HERE+%7B+%3Fs+%3Fp+%3Fo+%7D&limit=100&infer=true&Accept=application%2Fjson.没有访问控制 - 允许 - 原产地标头的请求的资源present。原产地'空',因此没有允许访问。
 

解决方案

特定的错误是(据我所知)Chrome浏览器特有的,有做的事实,Chrome浏览器无法识别应用程序/ sparql-结果+ JSON 作为脚本兼容的媒体类型。要解决此问题,替换应用程序/ JSON 中的MediaType在请求你接受参数。

更一般地,我要指出,你在这里做什么不是CORS相关。 CORS是有关添加一组HTTP头到每个请求/响应,以允许从浏览器跨域脚本调用。在这里,你正在使用JSONP回调,这是一个的不同的的(年龄大了,稍微不太安全)的机制来实现同样的目的。

FWIW芝麻服务器目前还不支持CORS头,虽然这是待办事项清单上: HTTPS ://openrdf.atlassian.net/browse/SES-1757

I am trying to retrieve data from a Sesame Triplestore using ajax. This is probably a CORS issue and I am trying to resolve it using CORS Filter. Is my assumption correct or do I need to change something in the code?

$(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);
        });
    }

I get the following error in the chrome console:

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

If I replace application/sparql-results+json with application/json, the error stays the same.

If I change the dataType: to "json" instead of "jsonp", the error changes to:

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. 

解决方案

The specific error is (AFAIK) Chrome-specific, and has to do with the fact that Chrome does not recognize application/sparql-results+json as a scripting-compatible media-type. To fix this, replace the mediatype with application/json in your Accept parameter in the request.

More generally, I should point out that what you're doing here is not CORS-related. CORS is about adding a set of HTTP headers to each request/response to allow cross-domain scripting calls from browsers. Here, you are using JSONP callbacks, which is a different (older, and slightly less secure) mechanism to achieve the same end.

FWIW Sesame Server currently does not yet support CORS headers, though this is on the ToDo list: https://openrdf.atlassian.net/browse/SES-1757 .