我使用ColdFusion和jQuery Ajax得不到返回数据

时间:2011-12-07 18:54:48

标签: ajax jquery coldfusion

我的回复是空白的。我甚至硬编码回复。我使用firebug并使用参数复制位置,当我将其粘贴到浏览器中时会显示响应。

代码:

<script type="text/javascript">
$(document).ready(function(){
    $("#mySubmitButton").click(function(){
        var zipCodeFilter = $('input[name=zipCodeFilter]').val();
        var zipRadius = $('select[name=zipRadius]').val();
        var querystring = "zipCodeFilter="+zipCodeFilter+"&zipRadius="+zipRadius;
        $.ajax(
            {
                type: "POST",
                dataType: "json",
                url: "http://dev.lead-hub.com/datasource/dataAccess.cfc?method=getZipCodes&returnformat=json",
                data: querystring,
                success: function(response){
                    var resp = jQuery.trim(response);//getting alot of whitespace in my return CFC method
                    alert(resp);
                    return false;
                    if (resp == 'true'){
                        $('#loginResponse').html("<span style='color: green;font-weight: bold; font-size: 15px;'>Success!!</span>");
                        // you'll want to put your code here to move onto the next page. I would simply do a page refresh
                        // and continue with using the session's login data
                    }else{
                        $('#loginResponse').html("<span style='color: red;font-weight: bold; font-size: 15px;'>Failed!!</span>");
                    }
                    return false;
                    }
                }
            );
        });
    }
);

CFC代码:

<cffunction name="getZipCodes" access="remote" returnType="string">
    <cfargument name="zipCodeFilter" required="true" type="numeric">
    <cfargument name="zipRadius" required="true" type="numeric">
    <cfset var local = {} />
    <cfset local.getZipCodes = "" />        
        <cfquery name="local.getZipCodes"  dataSource="#application.dns_live#">
            SELECT h.*
            FROM tbl_zipcodes g
            JOIN tbl_zipcodes h ON g.zipcode <> h.zipcode
            AND g.zipcode = '#arguments.zipCodeFilter#'
            AND h.zipcode <> '#arguments.zipCodeFilter#'
            WHERE g.GeogCol1.STDistance(h.GeogCol1)<=(#arguments.zipRadius# * 1609.344)
        </cfquery>
        <cfset local.returnString = "Good" />
    <cfreturn local.returnString />
</cffunction>

1 个答案:

答案 0 :(得分:2)

由于您在firebug中获得了空白响应,但是当您直接在浏览器中访问URL时,您会看到所期望的值,这可能会让我觉得可能存在缓存问题。尝试在您的ajax设置中添加“cache:false”:

$.ajax({
    cache: false,
    type: "POST",
    dataType: "json",
    url: "http://dev.lead-hub.com/datasource/dataAccess.cfc?method=getZipCodes&returnformat=json",
    data: querystring,
    success: function(response){
        var resp = jQuery.trim(response);//getting alot of whitespace in my return CFC method
        alert(resp);
        return false;
        if (resp == 'true'){
            $('#loginResponse').html("<span style='color: green;font-weight: bold; font-size: 15px;'>Success!!</span>");
            // you'll want to put your code here to move onto the next page. I would simply do a page refresh
            // and continue with using the session's login data
        }else{
            $('#loginResponse').html("<span style='color: red;font-weight: bold; font-size: 15px;'>Failed!!</span>");
        }
        return false;
    }
});

修改

因为那不是它 - 我想到了另一个想法。您说当您在URL中粘贴参数并直接请求它时,它适用于您。这意味着您正在使用GET请求请求您的方法,并在URL范围中传递参数。这与您的ajax请求不同,因为它是类型:“POST”。尝试更改你的ajax键入:“GET”,看看你是否开始回来。