How can I send a javascript object to a remote CFC component

I created a javascript object

var spanglist = { one: q1, two:q2, three:q3, four: q4}; 

I create a jjery ajax object to send data to CFC:

 $.ajax({ url: 'gridly/components/pay.cfc', type:"POST", dataType:' json', data: {method: "structFromJSobjt", returnFormat:"json", jsStruct: spanglist} }); 

in my cfc I have the following simple code:

 <cffunction name="structFromJSobj" access="remote" output="false" > <cfargument name="jsStruct" required="true" default="" /> <!--- AT this point I would like to work with the data contained in the jsStruct object. I can't access the data regardless of the typeI make the cfargument ---> </cffunction> 

Can someone play me in the direction of a data game when he is at work.

+4
source share
1 answer

Personally, I would only make small changes. For instance:

 $.ajax({ url: 'gridly/components/pay.cfc', type:"POST", dataType:' json', data: {method: "structFromJSobjt", returnFormat:"json", jsStruct: JSON.stringify(spanglist)} }); 

And on the CF side:

 <cffunction name="structFromJSobj" access="remote" output="false" > <cfargument name="jsStruct" required="true" type="string" /> <cfset var cfStruct = DeserializeJSON(arguments.jsStruct)> <!--- now use your structure ---> </cffunction> 

The only thing to note is the selective availability of the JSON.stringify () method in some browsers. Therefore, I recommend getting json2.js from http://www.json.org/

+7
source

Source: https://habr.com/ru/post/1384042/


All Articles