CF-Slayer
CF-Slayer

Reputation: 169

I am getting no return data using ColdFusion and jQuery Ajax

I am getting a blank response. I even hard code a response. I use firebug and copy the location with parameters and it shows a response when i paste it in my browser.

Code:

<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;
                    }
                }
            );
        });
    }
);

Code For 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>

Upvotes: 1

Views: 1937

Answers (1)

Jake Feasel
Jake Feasel

Reputation: 16945

Since you're getting a blank response in firebug, but you see the value you expect when you access the URL directly in the browser, it strikes me as a possible caching issue. Try adding "cache: false" to your ajax setup:

$.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;
    }
});

edit

Since that wasn't it - another thought occurred to me. You say it works for you when you paste the parameters in your URL and request it directly. That means you're requesting your method with a GET request, passing the parameters in the URL scope. This is different than your ajax request, since that is type: "POST". Try changing your ajax to type: "GET" and see if you start getting something back.

Upvotes: 2

Related Questions