Coldfusion: passing a struct as String through url

后端 未结 2 865
半阙折子戏
半阙折子戏 2021-01-13 08:17

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
相关标签:
2条回答
  • 2021-01-13 08:35
    <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>
    
    0 讨论(0)
  • 2021-01-13 08:48

    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#" />
    
    0 讨论(0)
提交回复
热议问题