Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Coldfusion: passing a struct as String through url

Is there a simple way to serialize a single-level structure as a string for use in a url?

for example:

?key1=val1&key2=val2
like image 679
Daniel Avatar asked Jun 15 '12 23:06

Daniel


2 Answers

<cfscript>
// create simple struct
x = { a=1, b=2, c=3 };
WriteDump(x);

// serialize in JSON format and encode for URL transport
y = URLEncodedFormat( SerializeJSON(x));
WriteOutput( 'url: <a href="#SCRIPT_NAME#?z=#y#">#SCRIPT_NAME#?#y#</a>');

// now receive the URL variable and dump it
if ( StructKeyExists( url, 'z' )) {
    writeOutput( '<h3>URL Data:</h3>' );
    writeDump( DeserializeJSON( URLDecode( z)));
}
</cfscript>
like image 154
BKK Avatar answered Nov 10 '22 00:11

BKK


How does this look?

<cfset tmpStruct = {"firstItem" = "one", "secondItem" = "two"} />

<cfset myUrl = "http://domain.com/file.cfm?" />

<cfloop list="#structKeyList(tmpStruct)#" index="i" >
    <cfset myUrl = myUrl & i & "=" & tmpStruct[i] & "&" />
</cfloop>

<cfset myUrl = left(myUrl,len(myUrl)-1) />

<cfdump var="#myUrl#" />
like image 30
Paul Avatar answered Nov 09 '22 23:11

Paul