Querying Sesame triplestore using JavaScript

痞子三分冷 提交于 2019-12-12 01:49:04

问题


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. 

回答1:


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 .



来源:https://stackoverflow.com/questions/29785633/querying-sesame-triplestore-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!