我有以下jQuery Ajax
場景,其中webmethod返回字符串的集合。數組結果的jQuery Ajax回調
- 集合可以是空
- 集合可以爲非空,但是零個記錄。
- 收集具有一個或多個記錄
下面的代碼工作正常。它使用jQuery.isEmptyObject。當它不是Plain Object
時,建議不要使用isEmptyObject()
。
如何在不使用isEmptyObject()的情況下處理結果?
注意:ajax「結果」是「不明顯」。
參考:
CODE
//Callback Function
function displayResultForLog(result)
{
if (result.hasOwnProperty("d"))
{
result = result.d
}
if ($.isPlainObject(result)) {
alert('plain object');
}
else
{
alert('not plain');
}
if($.isEmptyObject(result))
{
//Method returned null
$(requiredTable).append('No data found for the search criteria.');
}
else
{
if (result.hasOwnProperty('length'))
{
//Set the values in HTML
for (i = 0; i < result.length; i++)
{
var sentDate = result[i];
}
}
else
{
//Method returned non-null object; but there is no rows in that
$(requiredTable).append('No data found for the search criteria.');
}
}
}
function setReportTable(receivedContext) {
var searchInput = '111';
$.ajax(
{
type: "POST",
url: "ReportList.aspx/GetReportsSentWithinDates",
data: '{"searchInput": "' + searchInput + '"}',
contentType: "application/json; charset=utf-8",
dataType: "json",
context: receivedContext, //CONTEXT
success: displayResultForLog
}
);
}
否....如果結果爲空,則會在檢查結果中出錯。長度 – Lijo
好點,我已經通過建議修改了檢查對象是否未定義。 – daveyfaherty
它是'!==「undefined」'或'!== undefined'? – Lijo