javascript对象/数组undefined vs empty

时间:2016-03-15 03:12:51

标签: javascript arrays coldfusion coldfusion-10

我有一个Coldfusion页面,它对一个返回查询的cffunction进行Ajax调用。

JS:

[{
    "id":"2",
    "name":"Project2",
    "description":"Project2 detail",
    "status":"n",
    "created_at":"2016-03-14 15:09:58",
    "updated_at":"2016-03-14 15:09:58"
}]

ColdFusion的:

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

填充元素。

我尝试了jQuery Object { COLUMNS: Array[14], DATA: Array[1] } ,但它不可靠。至少在这种情况下。隐藏/显示$.isEmptyObject()向用户显示候选人是否已被放置。

我正在运行Coldfusion v10 64位。

1 个答案:

答案 0 :(得分:1)

检查数组对象的长度,看它是否有任何数据,如

respose.DATA

此外,typeof运算符返回字符串值,因此如果typeof response.DATA的值未定义,则'undefined'将为var status=function(action){ switch(action) { case 'create': return $scope.submitdata(); break; default: alert(); } $scope.submitdata=function() { service.postdata($scope.formdata) .success(function(data){ $scope.response=data; return 'SUCCESS'; }) .error(function(error){ Console.log(error); return 'FAILURE'; })