chrippy256
chrippy256

Reputation: 11

JQuery not processing ColdFusion JSON

I have a simple cfc that queries a database and returns a JSON object

<cfcomponent>
<cfsetting showdebugoutput="false" enablecfoutputonly="true">
<cffunction name="getNew" access="remote" returntype="query" returnFormat="json" secureJSON="false">
    <cfquery datasource="#request.datasource#" name="qSelectNotes">
        SELECT TOP 3 university_ID, name FROM university
    </cfquery>
    <cfreturn qSelectNotes>
</cffunction>

The CFC is returning the following JSON as viewed in FireBug:

   {"COLUMNS":["UNIVERSITY_ID","NAME"],"DATA":[[1,"MIT"],[2,"EMORY"],[3,"UNC"]]}

My jQuery function continually returns the error

   "SyntaxError: JSON.parse: unexpected character".  

My function is below:

    $.ajax({
       url: "notes_DB_check.cfc?method=getNew",
       dataType: "json", 
       success: function (myData) {
       alert(myData.DATA[1][1]); 
           },
        error: function (request, status, error) {
           alert("REQUEST:\t" + request + "\nSTATUS:\t" + status + "\nERROR:\t" + error);
           }
      });

Upvotes: 1

Views: 146

Answers (3)

chrippy256
chrippy256

Reputation: 11

Thanks to Adam Cameron for pointing me in the right direction. The component and the JSON were valid and it turns out there was another jQuery function on the page that was interfering with this function.

Upvotes: 0

Azam Alvi
Azam Alvi

Reputation: 7055

Try this one into your js

 $.ajax({
   url: "notes_DB_check.cfc?method=getNew",

   success: function (myData) {
   var abc = JSON.parse(myData);
   alert(abc);// Here you will run a for loop and then populate values from json 
       },
    error: function (request, status, error) {
       alert("REQUEST:\t" + request + "\nSTATUS:\t" + status + "\nERROR:\t" + error);
       }
  });

Upvotes: 1

Chris Tierney
Chris Tierney

Reputation: 1549

Try setting output="false" as an attribute in your cfcomponent and cffunction tags.

Upvotes: 2

Related Questions