输出URL中的变量

roc*_*cky 0 coldfusion

我想在URL中以变量的形式打印我自己的日期:

我有以下网址:

<cfset urladdress = "https://abc.xyz.com/start_date=2013-04-01&end_date=2014-04-22&data_type=123"> 
Run Code Online (Sandbox Code Playgroud)

我已将日期设置如下:

<cfparam name="startdate" default="#DateFormat(dateAdd('d',-1,now()), 'yyyy-mm-dd')#">
<cfparam name="enddate" default="#DateFormat(dateAdd('d',0,now()), 'yyyy-mm-dd')#">
Run Code Online (Sandbox Code Playgroud)

现在,我正在尝试打印它,如下所示:

<cfset urladdress = "https://abc.xyz.com/start_date="<cfoutput>#startdate#</cfoutput>"&end_date="<cfoutput>#enddate#</cfoutput>"&data_type=123">
Run Code Online (Sandbox Code Playgroud)

我得到以下错误:

 Invalid CFML construct found on line 19 at column 123.
ColdFusion was looking at the following text:

<

The CFML compiler was processing:

    < marks the beginning of a ColdFusion tag.Did you mean LT or LTE?
    A cfset tag beginning on line 19, column 2.
Run Code Online (Sandbox Code Playgroud)

我是否需要使用URLEncoded格式函数?

Hen*_*nry 5

你不需要<cfoutput>在里面<cfset>

<cfset urladdress = 
  "https://abc.xyz.com/start_date=#startdate#&end_date=#enddate#&data_type=123">
Run Code Online (Sandbox Code Playgroud)

你应该使用encodeForURL()(cf10 + only)或urlEncodedFormat()来保证变量的安全.

如果您想encodeForURL()在CF9中使用,请查看:https: //github.com/misterdai/cfbackport

  • @ user3558091阅读我的回答.`encodeForURL()`只是CF10 +,除非你使用https://github.com/misterdai/cfbackport来获取CF9中的`encodeForURL()`.他们都做了非常相似的事情,但是`encodeForURL()`比较旧的`urlEncodedFormat()`更新并覆盖了更多的情况,但它通常足够好. (3认同)
  • 你不仅不需要在cfset中使用cfoutput,而且你不能像这样嵌套cfml标签. (3认同)