Simon
Simon

Reputation: 227

CFC JSON Output - Issue with displaying query results

This is a long question - with a lot of detail - so first up apologies for that - but I'm not sure how to ask this in a briefer way.

I have two CFC's, both are designed to return JSON from a query, and then I'm using jquery to display the results.

The first CFC looks like this:

<cfcomponent output="no">

<cffunction name="getRequests" access="remote" returntype="query">

        <cfset var status = #arguments.status#>

            <run some query>

            <cfreturn getRequests>

    </cffunction>

This is called and the result displayed using the following:

$.getJSON("getRequests.cfc?method=getRequests&returnformat=json&queryFormat=column",{"status":1}, function(res,code) {
                if(res.ROWCOUNT > 0){
                  for(var i=0; i<res.ROWCOUNT; i++) {
                    s = "<div class='dPostTxt'><img class='icon01' src='images/icon01.png' /> <h4>" + res.DATA.TITLE[i] + "</h4> <p class='txt01>Panel <br /><span>RFQ ID</span></p>"
                    + "<p class='txt02'>" + /*res.DATA.PANEL[i] +*/"<br /><span>" + res.DATA.JOB_ID[i] + "</span></p>"
                    + "<p class='txt03'><span>Responses</span>" + /*res.DATA.RESPONSES  +*/ "<br /><span>Due </span>" + res.DATA.REQUIRED_DATE[i] + "</p>"
                    + "<img class='sep02' src='images/sep01.gif'  /> <br /><div class='clr'></div></div>";
                  };

                } 
                else {
                  var s = "Sorry, nothing matched your search.";
                }
                $("#results").html(s);

                },"json");  
        })

which all works fine.

The second CFC looks like this:

<cfcomponent output="no">

<cffunction name="getContacts" access="remote" returntype="query">

    <cfset var alpha = #arguments.alpha#>

    <run some query>

    </cfquery>

    <cfreturn getContacts>
        </cffunction>

     </cfcomponent>    

This CFC is called by and the data displayed by the following:

$(".alphaindex").click(function(e) {      
                var item = $(this).attr("title");
                if(item == "")return
                $.getJSON("getContacts.cfc?method=getContacts&returnformat=json",{"alpha":item}, function(res,code){
                    if(res.ROWCOUNT > 0){
                        for(var i=0; i<res.ROWCOUNT; i++) {
                            s += "<h3 class='postTitle'>" + res.DATA.CONTACTFIRSTNAME[i] + res.DATA.CONTACTLASTNAME[i] + "</h3>"
                            + "<p class='postDesc'>" + res.DATA.CONTACTEMAIL[i] + "</p>"
                            + "<p class='postDesc'>" + res.DATA.CONTACTMOBILE[i] + "</p> <br class='clr' />"
                            };
                        s += "";
                        } 
                    else {
                        var s = "Sorry, nothing matched your search.";
                    }
                    $("#results").html(s);

                    },"json");

                    e.preventDefault(); 
                })

This is returning JSON data headers - but not data. I can't tell what the difference is, or where else I'm going wrong with the second example.

Any advice appreciated - apologies again for the length of the question.

Simon

Upvotes: 1

Views: 1148

Answers (2)

Simon
Simon

Reputation: 227

Got it - as expected (and almost as always) something simple that I had missed. I needed to add &queryformat=column into the second example.

$.getJSON("getContacts.cfc?method=getContacts&returnformat=json&queryFormat=column"

Thanks to Edward for the help!

Simon

Upvotes: 1

Edward M Smith
Edward M Smith

Reputation: 10627

This is a longshot, but your function name is "getContacts" and, presumably, based on your code snippet, your query is called "getContacts".

I'm wondering if there's some bizarre collision happening there. Try renaming your query (and changing the "return" line as well) to something totally unique (be sure to VAR scope it too!)

edit: blah - your first CFC does that too, and it works. Well, try it anyway, who knows...

Upvotes: 1

Related Questions