我想將用戶的輸入集成到SPARQL查詢中。這是我現有的代碼,但我無法實現它的工作。如何將用戶輸入添加到現有的SPARQL查詢中
JavaScript的外部JS文件:
$('#submit99').on('click', function(e) {
var userInput = $('#messageInput').val();
var query = "PREFIX own: <http://www.owl-ontologies.com/travel.owl#>" +
"PREFIX trvl: <http://www.owl-ontologies.com/travel.owl#>" +
"select distinct ?label where {" +
"?s a trvl:" + userInput + " ."
"OPTIONAL {" +
"?type rdfs:label ?label }" +
"}";
var endpoint = 'http://localhost:5820/finalProject/query';
var format = 'JSON';
$.get('/sparql',data={'endpoint': endpoint, 'query': query, 'format': format}, function(json){
var pre = $('<pre></pre>');
pre.text(JSON.stringify(json));
$('#linktarget99').html(pre);
});
});
我的表格:
<div class="header">
<div class="container">
<h1> Zoek de plek voor je favoriete activiteiten </h1>
<p class="beschrijving"> Wil je weten op welke plek jij bepaalde activiteiten kunt doen? Vul hieronder de activiteit in en wij zoek het voor je uit! <a href="tutorial.html"> tutorial </a> </p>
<form role='form'>
<div class='form-group'>
<textarea class='form-control' id='messageInput' placeholder='type some nifty schilderij' row='1'> </textarea>
</div>
</form>
<div><a id="submit99" class='btn btn-primary'>Zoek de locatie van het schilderij</a></div>
</div>
</div>
這是錯誤我從終端獲得:
127.0.0.1 - - [14/Oct/2015 20:02:52] "GET /sparql?endpoint=http%3A%2F%2Flocalhost%3A5820%2FfinalProject%2Fquery&query=PREFIX+own%3A+%3Chttp%3A%2F%2Fwww.owl-ontologies.com%2Ftravel.owl%23%3EPREFIX+trvl%3A+%3Chttp%3A%2F%2Fwww.owl-ontologies.com%2Ftravel.owl%23%3Eselect+distinct+%3Flabel+where+%7B%3Fs+a+trvl%3A++BedAndBreakfast.&format=JSON HTTP/1.1" 200 -
似乎在「?s trvl:」+ userInput +「。」之後錯過了「+」。 – cesare
謝謝。雖然沒有解決問題。我怎樣才能打印變量userInput,所以我可以看到它包含什麼? –
您可以在使用 console.log(userInput) 或快速調試使用警報(userInput) – cesare