Search code examples
javascriptjqueryjsonsesame

load .srj files with jQuery's ajax or getJSON


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!


Solution

  • 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>