本文介绍了如何从FW / 1中的操作返回JSON?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
FW / 1似乎面向返回完整的网页,如果需要JSON数据怎么办?典型的布局如下:
<!DOCTYPE HTML PUBLIC - // W3C // DTD HTML 4.01 Transitional // EN > < html> < head> < title>用户经理< / title> < link rel =stylesheettype =text / csshref =assets / css / styles.css/> < / head> < body> < h1>用户经理< / h1> < ul class =nav horizontal clear> < li>< a href =index.cfm>首页< / a>< / li> < li>< a href =index.cfm?action = user.listtitle =查看用户列表>用户< / a>< / li& < li>< a href =index.cfm?action = user.formtitle =填写表单以添加新用户>添加用户< / a& < li>< a href =index.cfm?reload = truetitle =重置框架缓存> Reload< / a>< / li> < / ul> < br /> < div id =primary> < cfoutput>#body#< / cfoutput> < / div> < / div> < / body> < / html>解决方案
我使用的代码覆盖 onMissingView() Framework.cfc的方法。
我将我的响应包含在一个名为 rc的变量中。 然后在Application.cfc中使用类似于此代码的函数onMissingView(rc){ if(structKeyExists(rc,'json'){ var response = getPageContext()。getresponse() response.setContentType('application / json'); return serializeJSON (rc.json); } else { //我们需要这个触发有效的onMissignView错误 raiseException(FW1.viewNotFound,Unable to find a #request.action#action。,#request.missingView#'does not exist。); } } / pre>我使用其他逻辑来做 cfdump rc.json 当请求不是一个AJAX请求,但这是缩小到最小。
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>解决方案
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.
更多推荐
如何从FW / 1中的操作返回JSON?
发布评论