如何在没有给出json值时显示警告框

编程入门 行业动态 更新时间:2024-10-23 13:39:02
本文介绍了如何在没有给出json值时显示警告框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在使用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值时显示警告框

本文发布于:2023-11-27 21:05:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639600.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!