如何从FW/1中的操作返回JSON?
FW/1似乎倾向于返回完整的网页,如果需要JSON数据怎么办?典型的布局如下所示:如何从FW/1中的操作返回JSON?,json,coldfusion,fw1,Json,Coldfusion,Fw1,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.
<!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>
用户管理器
用户管理器
#身体#
这样就可以了
<!--- 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>
#序列化JSON(rc.response)#
这样就可以了
<!--- 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>
#序列化JSON(rc.response)#
我使用的代码覆盖了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请求时,我使用其他逻辑执行
rc.json
的cfdump
。但这是缩小到最小值。我使用的代码覆盖了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请求时,我使用其他逻辑执行
rc.json
的cfdump
。但这已缩小到最小值。从FW/12.2开始,您可以调用:
variables.fw.renderData( "json", result );
在您的控制器中,它将执行您想要的操作。从FW/12.2开始,您可以调用:
variables.fw.renderData( "json", result );
在您的控制器中,它将执行您想要的操作。我喜欢您在视图目录中需要一个文件。我的方法需要一个空白的文件视图文件和一个布局。我也不喜欢在视图中有一个空文件,这就是我找到这个解决方案的原因。我有另一种方法,通过检查头信息来检查请求是否是AJAX请求,如果我们在“dev”环境中,如果是,则执行rc.json的cfdump,换句话说,它只返回json。如何停止布局运行?我通常通过单个控制器路由所有将返回json的请求,在该控制器的
before()
方法中,我执行request.layout=false代码>我喜欢您在视图目录中需要一个文件。我的方法需要一个空白的文件视图文件和一个布局。我也不喜欢在视图中有一个空文件,这就是我找到这个解决方案的原因。我有另一种方法,通过检查头信息来检查请求是否是AJAX请求,如果我们在“dev”环境中,如果是,则执行rc.json的cfdump,换句话说,它只返回json。如何停止布局运行?我通常通过单个控制器路由所有将返回json的请求,在该控制器的before()
方法中,我执行request.layout=false代码>