Coldfusion: passing a struct as String through url

三世轮回 提交于 2019-12-19 07:34:32

问题


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

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



回答2:


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


来源:https://stackoverflow.com/questions/11059219/coldfusion-passing-a-struct-as-string-through-url

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