admin管理员组

文章数量:1400024

I want to get the Abtract of english article of civil engineering from Dbdepdia in Javascript. this is what I tried but it fail.

<html>
<head>
<script src=".11.0/jquery.min.js"></script>
<style type="text/css">

</style>
</head>
    <script type="text/javascript">
    var url = "";
    var query = "\
     PREFIX dbpedia2: </>\
     PREFIX Abs: </>\
    SELECT ?abstract\
   WHERE {\
            ?s dbpedia2:Civil_engineeringe\"@en;\ Abs:abstract ?abstract\ 
    }";

this how I encode the url to pass it to ajaxx

 var queryUrl = encodeURI( url+"?query="+query+"&format=json" );
    $.ajax({
        dataType: "jsonp",  
        url: queryUrl,
        success: function( _data ) {
            var results = _data.results.bindings;
            for ( var i in results ) {
                var res = results[i].abstract.value;
                alert(res);
            }
        }
    });
</script>
    <body></body>

</html>

I want to get the Abtract of english article of civil engineering from Dbdepdia in Javascript. this is what I tried but it fail.

<html>
<head>
<script src="http://ajax.googleapis./ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<style type="text/css">

</style>
</head>
    <script type="text/javascript">
    var url = "http://dbpedia/sparql";
    var query = "\
     PREFIX dbpedia2: <http://dbpedia/resource/>\
     PREFIX Abs: <http://dbpedia/ontology/>\
    SELECT ?abstract\
   WHERE {\
            ?s dbpedia2:Civil_engineeringe\"@en;\ Abs:abstract ?abstract\ 
    }";

this how I encode the url to pass it to ajaxx

 var queryUrl = encodeURI( url+"?query="+query+"&format=json" );
    $.ajax({
        dataType: "jsonp",  
        url: queryUrl,
        success: function( _data ) {
            var results = _data.results.bindings;
            for ( var i in results ) {
                var res = results[i].abstract.value;
                alert(res);
            }
        }
    });
</script>
    <body></body>

</html>
Share Improve this question edited Apr 1, 2014 at 8:11 user3335188 asked Mar 31, 2014 at 18:46 user3335188user3335188 1091 silver badge6 bronze badges 4
  • Does the JavaScript console in your web browser show any outstanding errors? – summea Commented Mar 31, 2014 at 18:51
  • No. it show blank page – user3335188 Commented Mar 31, 2014 at 19:38
  • And you are sure that the SPARQL is valid, yes? – summea Commented Mar 31, 2014 at 22:09
  • that is my question. that is where I am looking for help, may be I am wrong – user3335188 Commented Mar 31, 2014 at 22:38
Add a ment  | 

2 Answers 2

Reset to default 6

I use a different approach for multiline strings, and i use it directly for SPARQL query writing against DBPedia.

var query = [
 "PREFIX dbpedia2: <http://dbpedia/resource/>",
 "PREFIX Abs: <http://dbpedia/ontology/>",
 "SELECT ?abstract",
 "WHERE {",
    "?s dbpedia2:Civil_engineeringe\"@en;",
    "Abs:abstract ?abstract",
 "}"
].join(" ");

I do it this way because that allows me to adjust the line separator if encoding issues arise, and also it allows me to easily ment lines out if necessary.

Now, once i need to run the query, i encode the query itself and append that to the URL.

Be careful with how you are wrapping the entire query string, because it might be encoding the keys, values, and equals sign as escaped characters.

I do it this way:

var queryUrl = url+"?query="+ encodeURIComponent(query) +"&format=json";

The encoding seems okay, but your original SPARQL / JavaScript does not look Okay to me.

var query = "\
 PREFIX dbpedia2: <http://dbpedia/resource/>\
 PREFIX Abs: <http://dbpedia/ontology/>\
 SELECT ?abstract\
 WHERE {\
         ?s dbpedia2:Civil_engineeringe\"@en;\ Abs:abstract ?abstract\ 
 }";

Does not result in a valid JavaScript string as there is a space after '?abstract\', meaning you're escaping a space character. Check out this question relating to multiline JavaScript strings: Creating multiline strings in JavaScript.

Moreover, the SPARQL query is plain wrong at the moment. Try to build it out and test it first here and take a look at the spec.

本文标签: sparqlhow to query Dbpedia in JavascriptStack Overflow