2

Can anybody show me an example of how to load the .srj files that result from querying a Sesame SPARQL endpoint using jQuery's getJSON? I've tried setting the Accept header and other tricks but I still see the 200 code and apparently no error, but the content of the file is not loaded.

$.getJSON("http://localhost:8090/openrdf-sesame/repositories/myrepo?queryLn=SPARQL&query=QUERY&limit=none&infer=true&Accept=application%2Fsparql-results%2Bjson",
{
},
function(data) {
   alert('data = ', data);
});

I've tried something like this and countless other variants and it still doesn't work. I have to mention that I tested both cases:

  1. Tomcat is not mounted in Apache, case in which we have a cross-domain request - and I tried setting everything that was needed in the browser;
  2. Tomcat mounted in Apache - which as far as I know did not required anything else to work, but still no success.

Here is the Request Header:

Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip, deflate
Accept-Language:en-us,en;q=0.5
Connection:keep-aliveHost:localhost:8090
Origin:http://localhost
Referer:http://localhost/d3v280/examples/ablodvis/localtest.html
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0

Here is the Response Header:

Content-Disposition:attachment; filename=query-result.srj
Content-Language:en-US
Content-Type:application/sparql-results+json;charset=UTF-8
Date:Mon, 28 May 2012 14:06:06 GMT
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked

As you can see I do get a result in the query-result.srj file, but I don't know how to access it. I would very much prefer the first version to work, but apparently I am missing something. All the similar getJSON requests worked.

Here is the request that almost works:

 $.ajax({

        beforeSend: function(xhrObj){
                      xhrObj.setRequestHeader("Accept","application/sparql-results+json");
                },

        dataType: 'jsonp',
        url: queryUrl,
        success: function(data) {
          // callback code here
          console.log("source: " + data.length)
          alert('success!');
        }
      });

However it throws an "invalid label error" in Firefox, while in Chrome it doesn't throw any error, but as I see on the second line of my query-results.srj file it shows Uncaught SyntaxError: Unexpected token :. Here is how the first lines of the response look like:

{
"head": {
    "vars": [ "s", "p", "o", "r" ]
}, 
"results": {
    "bindings": [ ...

This time I am able to see the request done successfully and see it in my browser (at least in debug mode in both Chrome and Firefox). Should I understand that the jsonp trick doesn't work with Sesame? If I take the answer from Sesame, copy it in a file, rename it file.js and load it with $.getJSON it works ok...I don't want to have any server-side code for this application, just to process the result of a SPARQL query directly. I've easily setup up the rest of the sources (WorldBank, DBPedia, and others) through $.getJSON or $.ajax.

Best regards!

paxRoman
  • 2,064
  • 3
  • 19
  • 32
  • 1
    I just found this: http://code.google.com/p/rdf-spark/issues/detail?id=1 – paxRoman May 28 '12 at 19:25
  • if that fixed your problem, feel free to answer your own question and accept the answer (so it's easy for others to see what the solution is). – Jeen Broekstra May 28 '12 at 20:49
  • it's still not fixed, that's why it is in the comment...I did however edited the question to reflect current state – paxRoman May 28 '12 at 21:16
  • I'm not very experienced with jQuery, so sorry if this is stupid question, but: what happens if you set the datatype to 'json' instead of 'jsonp'? – Jeen Broekstra May 28 '12 at 22:26
  • Still doesn't work. The datatype is "json" if you are issuing a normal json request or "jsonp" (see http://stackoverflow.com/questions/2067472/please-explain-jsonp and http://json-p.org/). A json request will just exit with code 200 when trying it with jQuery, while a jsonp request will work, but throw an error on the second line of the query-results.sjr file. Now since I did not change anything on the server side, because I don't need/want to do that, the only explanation seems to be that Sesame does not support jsonp, but why does the normal request not work is hard to get. – paxRoman May 29 '12 at 08:22
  • as jsonp exposes you to all sorts of attacks, I would rather use "json" directly if only there would be a way to access it directly with jQuery. I have no problem getting the .srj file back with CURL or Python, and it works like charm, except that for this application I don't need any sever side code. I have also tried to wrap everything in paranthesis and do eval(obj) but that still doesn't work. Here is another link mycodefixes.blogspot.com/2012/01/json-and-jsonp.html. This whole jsonp is just because ajax call do not usually allow cross-domain calls. – paxRoman May 29 '12 at 08:58

1 Answers1

4

I have just written my own simple JQuery script to test this, and it seems to all work as expected. It took me while to get it to work, but mostly this had to do with the fact that my script was not running on the same server as Sesame.

Update to answer your question about jsonp: since release 2.7.0, Sesame does support JSONP callbacks (see SES-1019 ).

But provided the JQuery script runs on the same host as the sesame server, the following script outputs the query result as a table. I haven't quite figured what I'm doing differently from you, and I don't claim that this is the most efficient to process the reuslt, but at least this works, so I hope this will be useful.

Script:

      $(document).ready(function() {
      $.ajax({
                url: 'http://localhost:8080/openrdf-sesame/repositories/test',
                dataType: 'json', 
                data: { 
                    queryLn: 'SPARQL',
                    query: "SELECT * WHERE { ?s ?p ?o }", 
                    limit: 'none',
                    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);
        });
    }

HTML body contains an empty table:

  <table id="result" border=1>
  <thead/>
     <tbody/>
  </table>
Jeen Broekstra
  • 21,642
  • 4
  • 51
  • 73
  • Yes, it works at least on the same domain. It's enough for the moment to finish my demo. There are some differences in your approach: for example - you set the query language and the query directly in the data section of the request; then your query is not directly encoded as mine was. Since I tried cross-domain most of the time it's normal that it didn't worked without jsonp support. Maybe you will consider adding jsonp support in Sesame in the future or some other way to support cross-domain queries as Linked Data applications might need it. – paxRoman May 30 '12 at 08:43
  • 1
    JSONP callback support has been added in Sesame 2.7.0. As an aside, just to be clear: the way I set querylanguage and query in the data section makes no difference in the actual request being sent, it's just easier to read and edit. I also tested with all params directly encoded in the url, which gave the same result. – Jeen Broekstra Sep 02 '14 at 10:48