tab character causing ajax coldfusion component to fail, 500 Internal Server Error

642 Views Asked by At

I have a form that users will copy and paste text from a word doc. This content may include a tab character. An on-click function handles the form submission using JSMX ajax call to a coldfusion remote function. The form is passed through the ajax call to the component.

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

This works very well unless a tab character is in the form content. If a tab in the content, I get a 500 Internal Server Error.

This is sample text submitted in the form.

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

This is the encoded text from Firebug that is posted to the function.

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

Using Firebug, I can see the content posted to the function is encoded. Tabs are %9. I can put the cfc in the action of the form and the function doesn't fail.

My workaround is to strip out the tabs using javascript before sending it to the function. However, I would like to understand why the tab is causing the 500 error and if there is anything that can be done to prevent this.

2

There are 2 best solutions below

2
On

Try this code:

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

You can replace out the tabs with a simple regex in the CF code before handing back.

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