为了账号安全,请及时绑定邮箱和手机立即绑定

如果 json solr 响应未定义或在 ajax 调用中为 null

如果 json solr 响应未定义或在 ajax 调用中为 null

慕田峪4524236 2023-10-04 17:01:20
在我的项目中,我使用的是 solr。在我的 jsp 页面中,我想显示 solr 核心的响应,但某些字段未定义或为空。我编写了一个 ajax 调用,将响应数据放入 html 文本框中。我想检查该字段是否未定义或为空。<script>function openForm(){    document.getElementById("myForm").style.display = "block";    var salesOrder="\"" + $("#sOrder option:selected").val()+ "\"";    document.getElementById("sOrder_popup").value=$("#sOrder option:selected").val();    var URL_PREFIX="http://localhost:8983/solr/StorageCore/select?q=strSO:"    var URL_MIDDLE="&rows=99999&start=0&wt=json"    var URL=URL_PREFIX+salesOrder;    var loc=document.getElementById("location_popup").value;    $.ajax({        url : URL,        dataType : 'json',        type:'get',        json : 'json.wrf',        success : function(data) {            var docs = JSON.stringify(data.response.docs);            var jsonData=JSON.parse(docs);            if(jsonData[0].strLocation[0]===undefined)                document.getElementById("location_popup").value="";            else                document.getElementById("location_popup").value=jsonData[0].strLocation[0];            //document.getElementById("submitted_popup").value=jsonData[0].strSubmitName[0];        },        });}function closeForm(){    document.getElementById("myForm").style.display = "none";}</script>我在上面的 ajax 调用中编写了 if 语句,但它仍然给我这个错误。Unable to get property 'strLocation' of undefined or null reference
查看完整描述

1 回答

?
慕神8447489

TA贡献1780条经验 获得超1个赞

错误抱怨的jsonData[0]undefined,但你正在检查strLocation[0]

if (jsonData[0] === undefined)

..或者可能更好:

if (jsonData.length === 0) {
    ...
}

因为这实际上表达了您正在做的事情(检查是否返回 0 个结果),而不是用来undefined检查数组索引是否存在。


查看完整回答
反对 回复 2023-10-04
  • 1 回答
  • 0 关注
  • 56 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信