FW / 1似乎是面向返回完整的网页,如果需要JSON数据?典型的布局如下:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>User Manager</title>
<link rel="stylesheet" type="text/css" href="assets/css/styles.css" />
</head>
<body>
<h1>User Manager</h1>
<ul class="nav horizontal clear">
<li><a href="index.cfm">Home</a></li>
<li><a href="index.cfm?action=user.list" title="View the list of users">Users</a></li>
<li><a href="index.cfm?action=user.form" title="Fill out form to add new user">Add User</a></li>
<li><a href="index.cfm?reload=true" title="Resets framework cache">Reload</a></li>
</ul>
<br />
<div id="primary">
<cfoutput>#body#</cfoutput>
</div>
</div>
</body>
</html>
答案 0 :(得分:7)
截至FW / 1 2.2,您可以致电:
variables.fw.renderData( "json", result );
在你的控制器中,它会做你想要的。
答案 1 :(得分:3)
我使用的代码覆盖了Framework.cfc的onMissingView()
方法。
我将响应包装在名为rc.json
的变量中,然后在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.");
}
}
当请求不是AJAX请求时,我使用其他逻辑来执行cfdump
rc.json
。但这可以缩小到最低限度。
答案 2 :(得分:1)
这样做
<!--- 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>