Skip to main content
Inspiring
October 16, 2012
Answered

cfc jquery load issue

  • October 16, 2012
  • 2 replies
  • 5464 views

I am trying to load  data from a database using cfc and jquery but can not sort it out due to some issue on client side.

here is what I have so far:

.cfc


<cfcomponent output="false">

    <cffunction name="getData" access="remote" returntype="any" output="false">

      <cfquery name="test1" datasource="database">

           SELECT ID,NAME

           FROM table

       </cfquery>

       <cfset data = serializeJSON(test1)>

      <cfreturn data/>

    </cffunction>

</cfcomponent>

.cfm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

<script src="jscript/jquery-1.8.2.min.js"></script>

<script>

$(document).ready(function() {

    $("#loadLink").click(loadQuery)

});

function loadQuery() {

    $.ajax({

            url: "loaddata.cfc?method=getData",

            success:function(data){

                alert('data that function gets from the server:' + '\n' + '\n' + data + '\n' + '\n' +

                "everything is listed here");

        var columnMap = {}

        for (var i = 0; i < data.COLUMNS.length; i++) {

            columnMap[data.COLUMNS] = i   

        }

        var str = '<h1>People</h1>'

        for (var i = 0; i < data.DATA.length; i++) {

            str += '<b>'+data.DATA[columnMap.NAME]+'</b>'

            str += ' has a cool score of '+data.DATA[columnMap.ID]+'<br/>'

        }

        $("#content").html(str)

            }})

    return false

}

</script>

</head>

<body>

<p>

    <a href="" id="loadLink">Load Query</a>

</p>

<div id="content">

</div>

</body>

</html>

as you can see this is a simple script (I found it on the web and slightly changed it for testing purposes), the final one should be a bit complex.

here is an error firebug throws every time

TypeError: data.COLUMNS is undefined
for (var i = 0; i < data.COLUMNS.length; i++) {

I know this is a javascript question but I would really appreciate your help on this one.

    This topic has been closed for replies.
    Correct answer pirula08

    now I have two working examples, the first works great and the second one is even better

    2 replies

    pirula08AuthorCorrect answer
    Inspiring
    October 22, 2012

    now I have two working examples, the first works great and the second one is even better

    Miguel-F
    Inspiring
    October 22, 2012

    Great!  Glad you got it working.

    Miguel-F
    Inspiring
    October 16, 2012

    Is your alert firing?  If so, what does it show you?  Can you post the output here?

    From this line:

    alert('data that function gets from the server:' + '\n' + '\n' + data + '\n' + '\n' +

                    "everything is listed here");

    pirula08Author
    Inspiring
    October 16, 2012

    yup every time and as I can see everything is normal on the server side

    here is the output

    data that function gets from the server:

    <wddxPacket version='1.0'><header/><data><string>{"COLUMNS":["ID","NAME"],"DATA":[["1","John"],["2","Peter"],["3","Susan"]]}</string></data></wddxPacket>

    everything is listed here

    Miguel-F
    Inspiring
    October 16, 2012

    Well there you go.  That's not valid JSON format.  It is valid between the <string> tags but not with the wddx stuff.

    Try adding the returnformat on your cfc method and instead of returning "any" set it to return "string"

    <cffunction name="getData" access="remote" returnformat="json" returntype="string" output="false">

    Let us know what that returns.

    Also, you may no longer need to use the SerializeJson() function as I think the returnformat will do that for you.  Test to verify.