简体   繁体   English

使用jQuery的ajax或getJSON加载.srj文件

[英]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 ? 谁能给我展示一个示例,说明如何加载使用jQuery的getJSON查询芝麻SPARQL端点而生成的.srj文件吗? 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. 我试过设置Accept标头和其他技巧,但是我仍然看到200代码,并且显然没有错误,但是文件的内容未加载。

$.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; Tomcat没有安装在Apache中,在这种情况下我们有跨域请求-我尝试设置浏览器中所需的所有内容;
  2. Tomcat mounted in Apache - which as far as I know did not required anything else to work, but still no success. Tomcat安装在Apache中-据我所知,不需要其他任何工作,但仍然没有成功。

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. 如您所见,我确实在query-result.srj文件中得到了结果,但是我不知道如何访问它。 I would very much prefer the first version to work, but apparently I am missing something. 我非常希望第一个版本可以工作,但是显然我缺少了一些东西。 All the similar getJSON requests worked. 所有类似的getJSON请求均有效。

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 : . 但是,它在Firefox中引发“无效标签错误”,而在Chrome中则不引发任何错误,但是正如我在query-results.srj文件的第二行看到的那样,它显示了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). 这次,我可以看到请求已成功完成,并可以在浏览器中看到它(至少在Chrome和Firefox中均为调试模式)。 Should I understand that the jsonp trick doesn't work with Sesame? 我应该了解jsonp技巧不适用于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. 如果我从芝麻答案,它在复制文件,将其重命名file.js及使用$ .getJSON它工作正常加载它...我不想有这个应用程序的任何服务器端代码,只是为了直接处理SPARQL查询的结果。 I've easily setup up the rest of the sources (WorldBank, DBPedia, and others) through $.getJSON or $.ajax. 我已经通过$ .getJSON或$ .ajax轻松设置了其他来源(WorldBank,DBPedia和其他来源)。

Best regards! 最好的祝福!

I have just written my own simple JQuery script to test this, and it seems to all work as expected. 我刚刚编写了自己的简单JQuery脚本来对此进行测试,并且看起来一切正常。 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. 我花了一些时间才能使它工作,但主要是因为我的脚本未与Sesame在同一服务器上运行。

Update to answer your question about jsonp: since release 2.7.0, Sesame does support JSONP callbacks (see SES-1019 ). 更新以回答有关jsonp的问题:从2.7.0版开始,Sesame确实支持JSONP回调(请参阅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. 但是,如果JQuery脚本与芝麻服务器在同一主机上运行,​​则以下脚本将查询结果输出为表。 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: HTML正文包含一个空表:

  <table id="result" border=1>
  <thead/>
     <tbody/>
  </table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM