How can I send javascript object to a remote CFC Component

不羁岁月 提交于 2019-12-06 04:03:15

问题


I have created a javascript object

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

I create the ajax jquery object to send the data to the 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 poit me in the direction to play with the data once it is in the cffunction.


回答1:


Personally, I would make only slight changes. For example:

$.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 one thing to note about this is the spotty availability of the JSON.stringify() method in some browsers. So I recommend getting json2.js from http://www.json.org/



来源:https://stackoverflow.com/questions/8336819/how-can-i-send-javascript-object-to-a-remote-cfc-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!