views:

343

answers:

4

I have the following

 $.ajax({        
  type: "POST",        
  url: "qry_invControl.cfm",        
  data: "p_sales_price=" + input.val() + "&action=getCashPrice",
  cache: false,       
  success: function(data) {    
     $("#cashPrice_"+num).html(data);       
  }    
  });

I need to modify my qry_invControl.cfm to return other stuff like terms, monthly payment besides the cashPrice. How do I modify my success funtion so that I could do something like:

 $("#cashPrice_"+num).html(data.cashPrice);
 $("#terms_"+num).html(data.terms);
 $("#monthlyPayment_"+num).html(data.monthlyPayment);

I will need to populate other divs to show the related data (terms,monthlyPayment) with one ajax call which will come from qry_invControl.cfm.

+1  A: 

You should look into passing data back via JSON. jQuery has a .getJSON function for that purpose.

JSON tutorial.

Artem Russakovskii
+1  A: 

You want to return you data as a JSON object:

{ cashPrice: 4.50, terms: 2, monthlyPayment: 50 }

You can also specify dataType: "json" in your $.ajax call so that jQuery automatically treats the returned value as JSON. Make sure you using quotations around any non-numeric values (2.50 is fine, $2.50 needs quotes).

Mathachew
+8  A: 

Specify the datatype as JSON.

$.ajax({        
  dataType: 'json', 
  type: "POST",        
  url: "qry_invControl.cfm",        
  data: "p_sales_price=" + input.val() + "&action=getCashPrice",
  cache: false,       
  success: function(data) {    
    $("#cashPrice_"+num).html(data.cashPrice);
    $("#terms_"+num).html(data.terms);
    $("#monthlyPayment_"+num).html(data.monthlyPayment);     
  }    

});

In your CFM page you can output JSON with the SerializeJSON() function.

<cfset data = structNew()>
<cfset data.cashPrice = 4.50> 
<cfset data.terms = 2>
<cfset data.monthlyPayment= 50>
<cfoutput>#serializeJSON(data)#</cfoutput>
Patrick McElhaney
That's what I did. Now, I have to find out how to get the values out of JSon object.
FALCONSEYE
When I hit the qry_invControl.cfm with some values, it displays:{"INTERESTRATE":5.99,"CASH_SALES_PRICE":21250,"MONTHLYPAYMENT":"$248.60","PROJECTNUMBER":44065.0,"TOTALMONTHS":140,"TYPE":"HIGH","SALESPRICE":25000.0}When I pass the same values from jQuery, I run into problems. If i do success: function(data) { alert(data.CASH_SALES_PRICE); It will display the cash_sales_price. If I try to alert MONTHLYPAYMENT, or TOTALMONTHS, I get "undefined" . why?
FALCONSEYE
Can't tell without seeing more code. It sounds like you're not getting back what you expect from ColdFusion. Are you watching the output in a debugger, such as Firebug?
Patrick McElhaney
ColdFusion page returns:{"INTERESTRATE":5.99,"CASH_SALES_PRICE":21250,"MONTHLYPAYMENT":"$248.60","PROJECTNUMBER":44065.0,"TOTALMONTHS":140,"TYPE":"HIGH","TIMESTAMP":"June, 22 2009 13:07:01","SALESPRICE":25000.0}If I do: success: function(data) { alert(data.INTERESTRATE); .... }I will see the interestrate. It feels like it doesn't like anything after MONTHLYPAYMENT. I do have firebug, but I don't see any javascript errors.
FALCONSEYE
is it an issue to deal with JS being case-sensitive? cf is making the key names uppercase
Sam Farmer
After hours of staring, I finally found out what was going on. It was <div id="projectNumber_#tempPHIID#"> <cfif project_number EQ "">   <cfelse> #project_number# </cfif> </div>For some reason, this created a json string like: {"CASH_SALES_PRICE":"$21,250.00","TYPE":"HIGH","PROJECTNUMBER":"\n\t\t\t\t\t\t 44065 \n ","INTERESTRATE":5.99,"SALESPRICE":25000.0} The queries in qry_invControl.cfm crapped out because of the weird chars. Once I moved the div around projectnumber, it worked. Wow-Ricky James
FALCONSEYE
+1  A: 

The best solution is to have your script format the return data as json, and add

dataType:'json',

to your jQuery ajax setup. Adding that line makes jQuery automatically parse the json string into a javascript object. From there you can do exactly what you want: data.cashPrice, data.terms, etc.

Gabriel Hurley