如何在没有给出json值时显示警告框 [英] How to display alert box when no json value is given

查看:71
本文介绍了如何在没有给出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: <http://www.w3.org/2000/01/rdf-schema#>",


  "PREFIX yago: <http://dbpedia.org/class/yago/>",
    "PREFIX type: <http://dbpedia.org/class/yago/>",
    "PREFIX prop: <http://dbpedia.org/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值时显示警告框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆