如何确定 CFC 函数内的 ReturnFormat 并以所选格式返回数据?
我正在 CFScript 中编写以下函数,我想确定请求的 ReturnFormat,并以适当的格式返回数据。请注意,我根本没有在函数中定义 ReturnFormat - 我依赖于在调用中设置它。
例如,调用此函数的 URL 类似于: http://localhost/com/Calendar.cfc?method=getCalendars&UserName=demo&returnFormat=json
/**
*
* @hint Returns All Calendar records for one user.
* @output false
*/
remote any function GetCalendars(required string Username) {
var data = [];
var success = false;
var message = "";
try {
query = new Query();
query.setDataSource(APPLICATION.DSN);
query.addParam(name = "username", value = Username, cfsqltype = "varchar");
query.setSQL("
SELECT idn, CalendarName, CalendarURL, CalendarColor
FROM Calendars
WHERE Username = :username
ORDER BY CalendarName, idn
");
result = query.Execute();
rs = result.getResult();
success = true;
message = "Success";
records = rs.RecordCount;
}
catch (any excpt) {
success = false;
message = "An error occurred while getting calendars for user: " & Username;
}
finally {
//TODO: If ReturnFormat = json, return a JSON string
//TODO: If ReturnFormat = wddx, returna WDDX object
//TODO: If ReturnFormat = plain, return an XML string
return rs;
}
} //end GetCalendars
现在,此方法将返回 ColdFusion 自动格式化的 JSON 字符串,如下所示:
{"COLUMNS":["IDN","CALENDARNAME","CALENDARURL","CALENDARCOLOR"],"DATA":[[1,"演示 日历 1","http:\/\/localhost\/calendar\/feeds\/demo1\/basic","#43cd80"],[2,"演示 日历 2","http:\/\/localhost\/calendar\/feeds\/demo2\/basic","#9a9cff"]]}
或者像这样的 WDDX 对象:
<记录集 rowCount='2' fieldNames='IDN,CALENDARNAME,CALENDARURL,CALENDARCOLOR' type='coldfusion.sql.QueryTable'><字段 name='IDN'><数字>1.0<数字>2.0<字段 name='CALENDARNAME'>
演示日历 1 演示 日历 2 http:\/\/localhost\/calendar\/feeds\/demo1\/basic http:\/\/localhost\/calendar\/ feed\/demo2\/basic #43cd80 #9a9cff
但当我设置时,它失败并出现“无效返回类型”错误returnFormat=plain
。
基本上我需要一种方法来测试 ReturnFormat。然后我可以编写自己的返回子例程,以按照我想要的方式返回 JSON 数据(小写名称任何人!-我知道如何做到这一点,顺便说一句,这不是这个问题的一部分)并以 XML 格式返回。
I am writing the following function in CFScript and I want to determine the requested ReturnFormat, and return the data in the appropriate format. Note that I have not defined the ReturnFormat in the function at all - I am relying on setting it in my call.
For example, the URL to call this function would be similar to:http://localhost/com/Calendar.cfc?method=getCalendars&UserName=demo&returnFormat=json
/**
*
* @hint Returns All Calendar records for one user.
* @output false
*/
remote any function GetCalendars(required string Username) {
var data = [];
var success = false;
var message = "";
try {
query = new Query();
query.setDataSource(APPLICATION.DSN);
query.addParam(name = "username", value = Username, cfsqltype = "varchar");
query.setSQL("
SELECT idn, CalendarName, CalendarURL, CalendarColor
FROM Calendars
WHERE Username = :username
ORDER BY CalendarName, idn
");
result = query.Execute();
rs = result.getResult();
success = true;
message = "Success";
records = rs.RecordCount;
}
catch (any excpt) {
success = false;
message = "An error occurred while getting calendars for user: " & Username;
}
finally {
//TODO: If ReturnFormat = json, return a JSON string
//TODO: If ReturnFormat = wddx, returna WDDX object
//TODO: If ReturnFormat = plain, return an XML string
return rs;
}
} //end GetCalendars
Right now, this method will return either a ColdFusion automatically formatted JSON string like this:
{"COLUMNS":["IDN","CALENDARNAME","CALENDARURL","CALENDARCOLOR"],"DATA":[[1,"Demo
Calendar
1","http:\/\/localhost\/calendar\/feeds\/demo1\/basic","#43cd80"],[2,"Demo
Calendar
2","http:\/\/localhost\/calendar\/feeds\/demo2\/basic","#9a9cff"]]}
OR a WDDX object like this:
<wddxPacket version='1.0'><header/><data><recordset rowCount='2'
fieldNames='IDN,CALENDARNAME,CALENDARURL,CALENDARCOLOR'
type='coldfusion.sql.QueryTable'><field
name='IDN'><number>1.0</number><number>2.0</number></field><field
name='CALENDARNAME'><string>Demo Calendar 1</string><string>Demo
Calendar 2</string></field><field
name='CALENDARURL'><string>http:\/\/localhost\/calendar\/feeds\/demo1\/basic</string><string>http:\/\/localhost\/calendar\/feeds\/demo2\/basic</string></field><field
name='CALENDARCOLOR'><string>#43cd80</string><string>#9a9cff</string></field></recordset></data></wddxPacket>
But it fails with "Invalid return type" error when I set returnFormat=plain
.
Basically I need to have a way to test for the ReturnFormat. Then I can write my own return subroutines to return JSON data formatted the way I want (lower case names anyone! - which I know how to do BTW, that's not part of this question) and in XML format.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不会检测 returnFormat 或对其执行任何操作。这不是重点。 returnFormat 告诉 CF 它应该如何包装你的结果。重复:你不用担心这个。时期。
因此,给出一个创建数组的 CFC 方法,您只需返回该数组。 CF,如果看到 returnformat=json,将处理将其转换为 JSON。
如果它看到 returnformat=plain,它将抛出一个错误(因为数组不能是纯字符串)。
这有道理吗?
哦 - 所以我看到你的最后一段。如果您想尝试自己进行退货,则不应依赖退货格式。这已被烘焙到 CF 中。我将构建我的 api,使其始终返回 JSON、句点,并进行您自己的格式化。如果在方法上设置 returnFOrmat=plain,它会告诉 CF 不要执行任何操作。只要返回一个字符串就可以了。
You do not detect or do anything with returnFormat. That isn't the point. returnFormat tells CF how it should wrap your results. Repeat: You do not worry about this. Period.
So give a CFC method that makes an array, you would just return the array. CF, if is sees returnformat=json, will handle converting it to JSON.
If it sees returnformat=plain, it will thrown an error (since an array can't be a plain string).
Does this make sense?
Oh - so I see your last paragraph. If you want to try to do your own return, you should not rely on returnformat. That's baked into CF. I'd build my api to just ALWAYS return JSON, period, and do your own formatting. If you set returnFOrmat=plain on the method, it tells CF to not do anything. As long as you return a string, then you will be ok.