我在使用Json的HTML页面中显示了SPARQL查询的结果,我的问题是当输入某个值并且查询没有显示结果时它应该显示一个警告框。我的代码如下:
I have displayed the result of a SPARQL query in a HTML page using Json, my question is when a certain value is entered and the query does not display a result it should display a alert box. My code is below:
HTML
<table id="results"> </table>查询脚本
var query = [ "PREFIX rdfs: <www.w3/2000/01/rdf-schema#>", "PREFIX yago: <dbpedia/class/yago/>", "PREFIX type: <dbpedia/class/yago/>", "PREFIX prop: <dbpedia/property/>", "SELECT ?name ?runtime", "WHERE {", "?film rdf:type dbo:Film.", "?film dbp:name ?name.", "?film dbo:director dbr:Peter_Jackson.", "} GROUP BY ?name ?runtime" ].join(" "); alert("this query: [" + query + "]"); var queryUrl = url + "?query=" + encodeURIComponent(query) + "&format=json"; console.log(queryUrl); $.ajax({ dataType: "jsonp", url: queryUrl, success: function (data) { console.log(data); // get the table element var table = $("#results"); // get the sparql variables from the 'head' of the data. var headerVars = data.head.vars; // using the vars, make some table headers and add them to the table; var trHeaders = getTableHeaders(headerVars); table.append(trHeaders); // grab the actual results from the data. var bindings = data.results.bindings; // for each result, make a table row and add it to the table. for (rowIdx in bindings) { table.append(getTableRow(headerVars, bindings[rowIdx])); } if (bindings.trim().length == 0) { alert("empty"); //IF BINDING IS EMPTY DISPLAY ALERT BOX } } });目前,如果绑定是空的,它只显示 trHeaders 。
As for now it does not display anything if bindings is empty, it just shows trHeaders.
如果我怎么能弹出一个警告框c $ c>绑定是空的还是< td> 为空? 希望这个问题得到理解。谢谢您的时间。
How can I make an alert box pop up if bindings is empty or if <td> is empty? Hope this question was understood. Thanks for your time.
推荐答案您可以通过以下几种方式实现这一目标:
You can do this several different ways:
如果绑定是您的对象的常量,但有时可能为空:
If bindings is a constant of your object, but may sometimes be empty:
if (data.results.bindings.length) { //exists } else { alert('goes here'); }如果绑定是并不总是在服务器的响应中设置:
If bindings is not always set in the response from the server:
if (data.results.hasOwnProperty('bindings')) { //exists } else { alert('goes here'); }更多推荐
如何在没有给出json值时显示警告框
发布评论