ColdFusion的:传递一个结构作为字符串通过网址(Coldfusion: passing a

2019-06-25 12:25发布

有没有一种简单的方法来序列化一个单级结构,为在URL中使用的字符串?

例如:

?key1=val1&key2=val2

Answer 1:

<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>


Answer 2:

这个怎么样?

<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#" />


文章来源: Coldfusion: passing a struct as String through url