我有一个 Coldfusion 页面,它对返回查询的 cffunction 进行 Ajax 调用。
JS:
function showPlaced(){
$.ajax({
type: 'POST',
url: 'cfc/basic.cfc?method=GetCandidatePlacedJson',
data: 'searchNum=' + <cfoutput>#searchnum#</cfoutput>,
dataType:'json',
error: function(xhr, textStatus, errorThrown) {
// show error
alert(errorThrown);
},
success: function(response2, textStatus, jqXHR) {
console.log (response2);
console.log(jQuery.isEmptyObject(response2.DATA));
if (typeof response2.DATA === undefined) {
$("#placeddiv").hide();
} else {
if ((response2.DATA[0][0] != '') && (response2.DATA[0][13] == '')) {
//alert(response2.DATA[0][0]);
$('#Date_Accepted').val(response2.DATA[0][9]) // date accepted
$('#Start_DateDspOnly').val(response2.DATA[0][10]) // start date
$('#Start_Salary').val(response2.DATA[0][11]) // starting salary
$('#Additional_compensation').val(response2.DATA[0][12]) // additional compensation
$('#placedcandidatespan').html(response2.DATA[0][0]) // candidate name
$('#utitlespan').html(response2.DATA[0][3]) // candidate Title
$('#worknumberspan').html(FormatPhoneNumberPlaced(response2.DATA[0][1])) // work phone
$('#homenumberspan').html(FormatPhoneNumberPlaced(response2.DATA[0][2])) // home phone
$("#placeddiv").show();
} else {
$("#placeddiv").hide();
}
}
}
});
};
冷聚变:
<cffunction name="GetCandidatePlacedJson" output="false" access="remote" returntype="query" returnformat="json">
<cfargument name="searchNum" required="Yes" type="string" default="">
<cfset var queryResults="">
<cfquery name="queryIResults" datasource="#request.dsn#" dbtype="ODBC">
SELECT ui.firstname + ' ' + ui.lastname as PlacedName,ui.Work_Phone,Home_Phone, usm.description as Utitle,ui.title_number,csi.gone,csi.search_number
,csi.individual_number, csi.date_of_placement, cscc.accepteddate, cscc.startdate, cscc.startsalary, cscc.AdditionalCompensation, cscc.offerturneddown
FROM client_searches_individuals as csi
LEFT JOIN users_info as ui ON csi.individual_number = ui.id
Left join users_skill_mask as usm on usm.skill_number = 1 and usm.line_number = UI.title_number
Left Join client_searches_candidate_checklist as cscc on cscc.individual_number=csi.individual_number and cscc.search_number = csi.search_number
WHERE csi.search_number = #val(arguments.searchNum)# AND cscc.accepteddate > '' AND isnull(cscc.offerturneddown, '') = ''
</cfquery>
<cfreturn queryIResults>
</cffunction>
当函数没有返回任何行时,控制台日志显示;
Object { COLUMNS: Array[14], DATA: Array[0] }
和错误;
TypeError: response2.DATA[0] is undefined
当函数返回一行时,控制台日志显示;
Object { COLUMNS: Array[14], DATA: Array[1] }
并且元素被填充。
我试过 jQuery $.isEmptyObject()
但它不可靠。至少在这种情况下。 hide/show div
向用户显示候选人是否已被放置。
我正在运行 Coldfusion v10 64 位。
最佳答案
检查数组对象的长度,看是否有类似的数据
if (!response2.DATA || !response2.DATA.length) {
$("#placeddiv").hide();
} else {
}
此外,typeof运算符返回一个字符串值,因此如果 respose.DATA
的值未定义,则 typeof response.DATA
将为 'undefined'
关于javascript对象/数组未定义与空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36002022/