3 Replies - 448 Views - Last Post: 06 March 2017 - 11:51 AM

#1 midasxl  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 207
  • Joined: 03-December 08

How to get values from JSON returned from ajax call

Posted 24 February 2017 - 11:17 AM

Hello and thanks for your time,

I have the following $.ajax call to a function in a ColdFusion CFC file:

The call is initiated by a button click that passes an id (trigger).

$.ajax({
     type: 'POST',
     url: 'models/global_dialog_data.cfc?method=read_global_dashboard_details&returnformat=json&queryformat=struct,
     data: {p_ci:trigger}, // id passed to coldfusion function database call
     success: function(data){
          $("#content").html(data);
     },
     error: function(errorThrown){
          alert("The error is: " + errorThrown);
     }



The ColdFusion function (global_dialog_data.cfc) gets a single record from the database:

<cffunction name="read_global_dashboard_details" access="remote" returntype="query">
     <cfargument name="p_ci" required="yes">
     <cfstoredprocedure procedure="xxxxxxxxxxxx" datasource="xxxxxx">
          <cfprocparam type="in" cfsqltype="cf_sql_varchar" value="#p_ci#" dbvarname="p_ci">
          <cfprocresult name="getGlobalDetails">
     </cfstoredprocedure>
     <cfreturn getGlobalDetails>
</cffunction>



The call always returns successful, so the success function in the $.ajax call runs and populates a div (id='content') on my page so I can see what is coming back. It's a single record from the database and it looks like this:

//[{"CI":"M001","TITLE":"Test Modal Dialog","CONTENT":"<p><strong>This is the contents of this dashboard details modal dialog.</strong></p>","HEIGHT":100,"WIDTH":100}]



This looks like an array with one struct inside. My question is how do I extract the value of "CI", "TITLE", "CONTENT", "HEIGHT", and "WIDTH"?

I've tried:

data.CI
data[0]["CI"]
data["CI"]
etc.

I'm just trying everything because what i have found online is overwhelming. Anyways, I can't get to the values with all that I have tried.

Also, I do not know why there are two forward slashes at the beginning of the array.

Any insight you may be able to provide will be much appreciated.

Cheers!!

Is This A Good Question/Topic? 0
  • +

Replies To: How to get values from JSON returned from ajax call

#2 ArtificialSoldier  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1841
  • View blog
  • Posts: 5,793
  • Joined: 15-January 14

Re: How to get values from JSON returned from ajax call

Posted 24 February 2017 - 11:34 AM

If those slashes are actually returned by the server then you need to figure out why and remove them, that would cause a problem with JSON parsing. You can use your browser's developer tools to look at the ajax request and check the response to verify that. Otherwise, either data[0]["CI"] or data[0].CI will work to access those values, so if that's not working then there's probably an issue with the response from the server.
Was This Post Helpful? 0
  • +
  • -

#3 midasxl  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 207
  • Joined: 03-December 08

Re: How to get values from JSON returned from ajax call

Posted 01 March 2017 - 07:28 AM

Thanks ArtificialSoldier for your reply and suggestions. Dev tools have revealed that I am indeed dealing with a parse error. I'll check with our DBA's to see if they can figure out where those leading slashes are coming from.

Cheers!!
Was This Post Helpful? 0
  • +
  • -

#4 midasxl  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 207
  • Joined: 03-December 08

Re: How to get values from JSON returned from ajax call

Posted 06 March 2017 - 11:51 AM

OK, let's close this up.

So we never figured out where the leading slashes are coming from. Perhaps in the transformation?

Anyways, the slashes were causing a parse error. So I got rid of them:

success: function(data){
data = data.replace("//", "");
data = $.parseJSON(data);



Now I am able to access the values like this:

title = data[0]["TITLE"];
content = data[0]["CONTENT"];
etc.



Thanks!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1