当name包含连字符/破折号时,从Form范围加载ColdFusion值

时间:2016-02-05 10:46:45

标签: coldfusion

我们正在升级到Google的reCaptcha,并在提交期间向表单添加名称为“g-recaptcha-response”的字段。我们需要使用以下代码验证对Google的响应:

<cfhttp url="https://www.google.com/recaptcha/api/siteverify" method="post" result="captchaResult">
  <cfhttpparam type="formfield" name="secret" value="SECRET"> 
  <cfhttpparam type="formfield" name="response" value="#Form.g-recaptcha-response#"> 
  <cfhttpparam type="formfield" name="remoteip" value="#CGI.REMOTE_ADDR#"> 
</cfhttp>

但是,#Form.g-recaptcha-response#Element G is undefined in FORM.

是否有另一种方法可以引用Form范围以允许连字符?

2 个答案:

答案 0 :(得分:4)

如果表单变量不存在,那将导致错误。试试这个:

<CFSET Form.Response = "">
<CFIF StructKeyExists(Form, "g-recaptcha-response")>
   <CFSET Form.Response = form["g-recaptcha-response"]>
</CFIF>

昨天我写了一个reCAPTCHA v2 UDF。我想支持sToken,所以我可以在多个网站上使用相同的API密钥。

http://gist.github.com/JamoCA/c4e83ca402bd6175a1d7

答案 1 :(得分:3)

刚刚找到解决方案:

  <cfhttpparam type="formfield" name="response" value="#Form["g-recaptcha-response"]#">