How do I return JSON from an action in FW/1?

荒凉一梦 提交于 2019-12-02 05:00:28

I have used code that overrides the onMissingView() method of Framework.cfc.

I wrap my response up in a variable named rc.json then use code similar to this in my Application.cfc.

function onMissingView( rc ){
    if( structKeyExists( rc, 'json' ){
        var response = getPageContext().getresponse()
        response.setContentType( 'application/json' );
        return serializeJSON( rc.json );
    }
    else{
        //we need this to fire off valid onMissignView error.
        raiseException( "FW1.viewNotFound", "Unable to find a view for '#request.action#' action.", " '#request.missingView#' does not exist.");
    }
}

I use other logic to do a cfdump of rc.json when request is not an AJAX request. But this is scaled down to bare minimum.

As of FW/1 2.2, you can call:

variables.fw.renderData( "json", result );

in your controller and it will do what you want.

This will do it

 <!--- Load all variables into response rather than just rc --->
 <cfparam name="rc.response" default="#structNew()#">
 <cfparam name="rc.response.status" default="OK">

 <!--- Stop layouts from cascading --->
 <cfset request.layout = false>

 <cfsetting showDebugOutput="No">
 <cfheader name="Content-Type" value="application/json" />

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