tab字符导致ajax coldfusion组件失败,500内部服务器错误

时间:2012-05-09 21:58:12

标签: ajax coldfusion cfc cffunction

我有一个表单,用户可以从word doc复制和粘贴文本。该内容可以包括制表符。 on-click函数使用JSMX ajax调用coldfusion远程函数来处理表单提交。表单通过ajax调用传递给组件。

<form name="test">
<textarea name="swPurpose"></textarea>
<input type="button" value=" Go " onclick="goForm(this.form);" />
</form>

function goForm(f){
var param=f; 
http('POST','testCFC.cfc?method=test',goForm_RTN,param);
}

<cfcomponent output="false">
<cffunction name="test" access="remote" output="false">
<cfset rtn=structNew()/>
<cfsavecontent variable="rtn.html">
<cfoutput><p>#form.swPurpose#</p></cfoutput>
</cfsavecontent>
<cfreturn rtn />
</cffunction>
</cfcomponent>

除非表单内容中包含制表符,否则此操作非常有效。如果内容中有一个标签,我会收到500内部服务器错误。

这是表格中提交的示例文本。

1   This is text
2   This is text
3   This is text

这是Firebug中发布到函数的编码文本。

swPurpose=1%9This%20is%20text%0a2%9This%20is%20text%0a3%9This%20is%20text&btn=%20OnClick%20,%20Submit%20

使用Firebug,我可以看到发布到该功能的内容已经过编码。标签是%9。我可以将cfc放在表单的操作中,并且函数不会失败。

我的解决方法是在将标签发送到函数之前使用javascript删除标签。但是,我想了解为什么选项卡导致500错误,以及是否有任何可以做的事情来防止这种情况。

2 个答案:

答案 0 :(得分:0)

试试这段代码:

function goForm(f){
    var param = escape(f);//Or also encodeURI(f) or even encodeURIComponent(f)
    http('POST','testCFC.cfc?method=test',goForm_RTN,param);
}

答案 1 :(得分:0)

您可以在交还之前用CF代码中的简单正则表达式替换掉标签。

<cfcomponent output="false">
    <cffunction name="test" access="remote" output="false">
        <cfargument name="form">
        <cfset var rtn=structNew()/>

        <cfsavecontent variable="rtn.html">
            <cfoutput><p>#ReReplace(form.swPurpose, "\t", "&nbsp;&nbsp;", "ALL")#</p></cfoutput>
        </cfsavecontent>
        <cfreturn rtn />
    </cffunction>
</cfcomponent>