How to display alert box when no json value is given

前端 未结 1 1202
梦毁少年i
梦毁少年i 2021-01-16 11:38

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 dis

1条回答
  •  隐瞒了意图╮
    2021-01-16 12:09

    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');
    }
    

    0 讨论(0)
提交回复
热议问题