Is there a way to prevent cfchart from forcing js into response content?

白昼怎懂夜的黑 提交于 2020-03-03 06:28:20

问题


Update: reported bug, votes appreciated https://tracker.adobe.com/#/view/CF-4200017

We are migrating from ColdFusion 10 to 2016. It appears the only hurdle with compatibility is the change in charting system. For full page requests this is generally fine but in some spots we generate a chart in png format, capture the content generated and strip out some js that is generated simultaneously and return the result in json as part of a page update. A problem we are running into is that CF is forcing javascript in the request response separate from the actual content being output causing json responses to be invalid.

It seems the js is being inserted using an approach like the cfhtmlhead tag where it looks in the response for head tags, inserts code there if found, otherwise it just prepends the code to the entire request. For a response that should be json this is obviously a problem.

I already tried calling a flush before the chart generates but then I get the error "ColdFusion was unable to add the text you specified to the output stream. This is probably because you have already used a CFFLUSH tag in your template or buffered output is turned off" when the cfchart code is hit.

Any suggestions on how to tell CF to put the code inline or otherwise reset the content planned for insertion into the request output?

Example:

<cfsavecontent variable="ignore">
<cfchart format="png">
    <cfchartseries type="Bar" label="Numbers">
        <cfchartdata item="First" value="1">
        <cfchartdata item="Second" value="2">
    </cfchartseries>
</cfchart>
</cfsavecontent>

<cfcontent reset="true" />
test

Result:

<script type="text/javascript">/* <![CDATA[ */_cf_loadingtexthtml="<img alt=' ' src='/cf_scripts/scripts/ajax/resources/cf/images/loading.gif'/>";
_cf_contextpath="";
_cf_ajaxscriptsrc="/cf_scripts/scripts/ajax";
_cf_jsonprefix='//';
_cf_websocket_port=8579;
_cf_flash_policy_port=1243;
_cf_clientid='1D0DA2C606EC323ABBECA5B29A016CF0';/* ]]> */</script><script type="text/javascript" src="/cf_scripts/scripts/ajax/messages/cfmessage.js"></script>
<script type="text/javascript" src="/cf_scripts/scripts/ajax/package/cfajax.js"></script>
<script type="text/javascript" src="/cf_scripts/scripts/chart/cfchart-server.js"></script>

test

It is even an issue for charts used in cfdocument tags (just replace the cfsavecontent tags with cfdocument filename="test.pdf" to test). The chart gets added to the document on disk but then js gets output in the request response, killing our ajax requests that are meant to bring back the download link and other data.


回答1:


Found a workaround which is to to perform the chart generation in a thread. The output is then only the base chart content and not head content.




回答2:


This can be addressed with either of the following options for resetting the content that CF inserts automatically into the <head>:

<cfcontent reset="true" resethead="true">

or

<cfset getPageContext().getCFOutput().clearHeaderBuffers()>

These approaches are not documented (so possible to change in future version -- though unlikely) and these approaches are unlikely to be portable to other CFML engines.



来源:https://stackoverflow.com/questions/46731606/is-there-a-way-to-prevent-cfchart-from-forcing-js-into-response-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!