如何从 FW/1 中的操作返回 JSON? [英] How do I return JSON from an action in FW/1?
本文介绍了如何从 FW/1 中的操作返回 JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
FW/1 似乎面向返回完整的网页,如果需要 JSON 数据怎么办?典型的布局如下所示:
FW/1 seems to be oriented to returning complete web pages what if JSON data is needed? A typical layout looks like:
<!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>
推荐答案
我使用了覆盖Framework.cfc的onMissingView()
方法的代码.
I have used code that overrides the onMissingView()
method of Framework.cfc.
我将我的响应包装在一个名为 rc.json
的变量中,然后在我的 Application.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.");
}
}
当请求不是 AJAX 请求时,我使用其他逻辑来执行 rc.json
的 cfdump
.但这已缩小到最低限度.
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.
这篇关于如何从 FW/1 中的操作返回 JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文