如果 json solr 响应在 ajax 调用中未定义或为空
if json solr response is undefined or null in ajax call
在我的项目中,我使用的是 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
错误提示 jsonData[0]
是 undefined
,但您正在检查 strLocation[0]
。
if (jsonData[0] === undefined)
.. 或者可能更好:
if (jsonData.length === 0) {
...
}
因为这实际上表达了你在做什么(检查是否返回了 0 个结果),而不是使用 undefined
检查数组索引是否存在。
在我的项目中,我使用的是 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
错误提示 jsonData[0]
是 undefined
,但您正在检查 strLocation[0]
。
if (jsonData[0] === undefined)
.. 或者可能更好:
if (jsonData.length === 0) {
...
}
因为这实际上表达了你在做什么(检查是否返回了 0 个结果),而不是使用 undefined
检查数组索引是否存在。