抛出undefined的JQuery ajaxHandleResponse()不是函数

时间:2015-10-08 12:36:59

标签: jquery regex testing

使用JQuery发送AJAX请求后,我收到错误: undefined is not a function 虽然请求成功,但我从浏览器开发人员工具的服务器获得响应。

在尝试一些调试后,我发现了这个代码中存在的问题:

function ajaxHandleResponses( s, jqXHR, responses ) {

var ct, type, finalDataType, firstDataType,
    contents = s.contents,
    dataTypes = s.dataTypes;

// Remove auto dataType and get content-type in the process
while ( dataTypes[ 0 ] === "*" ) {
    dataTypes.shift();
    if ( ct === undefined ) {
        ct = s.mimeType || jqXHR.getResponseHeader("Content-Type");
    }
}

// Check if we're dealing with a known content-type
if ( ct ) {
    for ( type in contents ) {
        if ( contents[ type ] && contents[ type ].test( ct ) ) { // HERE IS WHERE THE EXCEPTION IS TRIGGERED
            dataTypes.unshift( type );
            break;
        }
    }
}

// Check to see if we have a response for the expected dataType
if ( dataTypes[ 0 ] in responses ) {
    finalDataType = dataTypes[ 0 ];
} else {
    // Try convertible dataTypes
    for ( type in responses ) {
        if ( !dataTypes[ 0 ] || s.converters[ type + " " + dataTypes[0] ] ) {
            finalDataType = type;
            break;
        }
        if ( !firstDataType ) {
            firstDataType = type;
        }
    }
    // Or just use first one
    finalDataType = finalDataType || firstDataType;
}

// If we found a dataType
// We add the dataType to the list if needed
// and return the corresponding response
if ( finalDataType ) {
    if ( finalDataType !== dataTypes[ 0 ] ) {
        dataTypes.unshift( finalDataType );
    }
    return responses[ finalDataType ];
}

}

未定义的函数是test,在contents[type].test(ct)调用, 我相信contents[type]应该是一个正则表达式,而不是它,在我的情况下它的值是"json",一个字符串。

有人对此有任何疑问吗? :) thx提前

编辑: 我的ajax电话:

$.ajax({
                type : "POST",
                url  : "/channel" ,
                data : '{ "username":"' + username + '" , "password":"' + password + '"}',
                complete : function(data, status, jqXHR){
                    $(".btn").prop('disabled',false);
                },
                success : function(data, status, jqXHR){
                    data = JSON.parse(data);
                    if(data.error != 0){
                        $(".alert-message").html("Sorry, Try again.");
                        $(".alert").css("display","block");
                        return;
                    }
                    window.location.assign("/fullcalendar?session="+data.session);
                },
                contentType: 'application/json',
                error: function(jqXHR, status, errorThrown){
                    if(errorThrown == "Forbidden"){
                        $(".alert-message").html("Wrong Password");
                        $(".alert").css("display","block");
                    }else{
                        console.log("error in connection " + errorThrown);
                        $(".alert-message").html("Sorry, We're having problems right now try again later.");
                        $(".alert").css("display","block");
                    }
                }
            });

0 个答案:

没有答案