亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如果 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 關注
  • 120 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號