我在使用以下代码时遇到问题。我正在寻找验证地址(城市、邮编、州)并将其与我们数据库中的内容(在 CFC 中调用)进行检查。当我为 Ajax 设置 dataFilter 时,它会进入错误并显示“parseerror”,并且我没有看到字符串 javascript 返回。当我删除 dataFilter 时,在控制台中我看到 javascript 字符串返回,但它总是进入成功设置的“else”。
有谁知道发生了什么?我究竟做错了什么?CFC 确实根据 Fiddler 正确返回(真/假)。
$.ajax({
type: "get",
url: "/component/validateLenderAddress.cfc",
data: {
method: "validateZipCityState",
returnFormat: "json",
zip:$('input[name*="zip"]').val(),
city:$('input[name*="city"]').val(),
state:$('input[name*="state"]').val()
},
async: false,
cache: false,
dataFilter: function(data) {
return $.parseJSON(data);
},
success:function(data) {
if (data) {
alert('Address Validated');
return true;
}
else {
alert('Address could not be validated');
return false;
}
},
error: function(xhr, ajaxOptions, thrownError) {
alert(ajaxOptions);
}
});
我的 CFC 是
<cfcomponent output="false" extends="component.Database" hint="validates lender address lendermaint.inc">
<cffunction name="validateZipCityState" output="false" returnType="struct" access="remote">
<cfargument name="zip" type="string" required="true">
<cfargument name="city" type="string" required="true">
<cfargument name="state" type="string" required="true">
<cfset var local = structNew()>
<cfquery name="local.zipCodeList" datasource="#getDSN()#" username="#getUsername()#" password="#getPassword()#">
SELECT TOP 1 1
FROM ZipCode
WHERE zipCode = <cfqueryparam cfsqltype="cf_sql_varchar" value="#trim(arguments.zip)#">
AND city = <cfqueryparam cfsqltype="cf_sql_varchar" value="#trim(arguments.city)#">
AND stateShort = <cfqueryparam cfsqltype="cf_sql_varchar" value="#trim(arguments.state)#">
</cfquery>
<cfreturn local.zipCodeList.recordCount EQ 1>
</cffunction>
谢谢!