2017-01-20 2 views
1

Json을 사용하여 HTML 페이지에 SPARQL 쿼리의 결과를 표시했습니다. 특정 값을 입력하면 표시되어야하는 결과가 쿼리에 표시되지 않습니다. 경고 상자. 내 코드는 다음과 같습니다 :json 값이 없을 때 경고 상자를 표시하는 방법

HTML

<table id="results"> 
    </table> 

쿼리 스크립트, 그냥 trHeaders을 보여줍니다 bindings가 빈 상태 (empty)의 경우 아무것도 표시되지 않습니다 지금으로

 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 
        } 


       } 
      }); 

.

bindings이 비어 있거나 <td>이 비어 있으면 어떻게 경고 상자가 튀어 나옵니까? 이 질문이 이해되기를 바랍니다. 시간 내 줘서 고마워.

+0

이 시도 (result.bindings) {} 다른 경고 ('빈') 여기서 당신이) (bindings.trim 확인하고 길이 == 0 쿼리 SCRI에. pt –

+0

사람들이 여기에서 숙제를하고있는 것처럼 보입니다. 같은 코드로 같은 질문을 던지면 지난번에 물었습니다. http://stackoverflow.com/questions/41617269/json-data-from-sparql-query-not-displaying-on-table and http://stackoverflow.com/questions/41602590/display-alert-box-when-table-element-has-no-text. 중복으로 표시되어야합니다. – AKSW

답변

2

당신이 여러 가지 방법으로 수행 할 수 있습니다

bindings 경우 개체의 일정,하지만 때로는 비어있을 수 있습니다 항상 서버의 응답에 설정되지 않은

if (data.results.bindings.length) { 
    //exists 
} else { 
    alert('goes here'); 
} 

bindings 경우 다음과 같은 경우

if (data.results.hasOwnProperty('bindings')) { 
    //exists 
} else { 
    alert('goes here'); 
} 
관련 문제