Jquery Ajax调用CFC无法正常工作

时间:2012-08-27 17:42:51

标签: jquery ajax coldfusion cfc


我遇到以下代码问题。我正在寻找一个地址(城市,邮政编码,州),并根据我们的数据库中的内容进行检查,该数据库在CFC中调用。 当我有Ajax的dataFilter设置时,它进入错误并显示“parseerror”,我没有看到字符串javascript返回。 当我删除dataFilter时,在控制台中我看到javascript字符串返回,但它总是进入成功设置的“else”。
有谁知道发生了什么?我究竟做错了什么? 根据Fiddler(真/假),CFC确实正确返回。

$.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>

谢谢!

2 个答案:

答案 0 :(得分:2)

不使用dataFilter而只设置{ dataType : "json" },而不需要使用dataFilter。另外,如果你在Content-type: application/json的响应上设置标题,jquery会自动将响应处理为json。

另外,请考虑删除异步,因为您已经有处理程序等待响应,这可能会锁定浏览器并移动您的错误并完成处理程序以使用jqXHR .done()和.fail()

答案 1 :(得分:2)

我将以下内容放入所有远程组件中:

<cfparam name="url.returnformat" default="json">
<cfparam name="url.queryformat" default="column">

这是我写的一个帮助我做ajax的插件:

!function($, window, undefined) {
    $.ajaxSetup ({
         cache: false // http://stackoverflow.com/questions/168963/stop-jquery-load-response-from-being-cached
    });
    $('#msg').ajaxStart(function() {
      $(this).empty().removeClass('alert alert-info');
    });
    $.fn.myAjax = function(myURL, mySettings) {
        var local = {};
        local.settings = {
            type: 'post',
            dataType: 'json',
            context:this[0]
        };
        local.settings = $.extend({}, local.settings, mySettings);
        local.XHR = $.ajax(myURL,local.settings);
        local.XHR.done(function(result) {
            if (result.MSG) {
                $('#msg').html(result.MSG).addClass('alert alert-error');
            }
        });
        local.XHR.fail(function(A,B,C) {
            $('#msg').html(C).addClass('alert alert-error');
        });
        return local.XHR;
    };
}(jQuery, window);

它允许你做的是在它返回时使用'this':

!function($, window, undefined) {
    var settings = {}
    settings.data = {};
    settings.data.method = 'Save';

    $('input:checkbox').on('change',function() {
        $(this).removeAttr('checked');
        settings.data.xxxID = $(this).val();
        var myPromise = $(this).myAjax('xxx.cfc',settings);
        myPromise.done(function(result) {
            if (!result.MSG) {
                $(this).prop('checked',true); // OK, it's been inserted.
                local.qryxxx = result.qry.DATA;
                local.qryxxx.RecordCount = result.QRY.ROWCOUNT;
                local.qryxxx.ColumnList = result.QRY.COLUMNS;
            }
        });
    });
}(jQuery, window);