【发布时间】:2013-02-25 18:19:39
【问题描述】:
我正在尝试使用带有 ColdFusion 后端的 AngularJS,但遇到了一些障碍。我正在使用 CF Art Gallery 数据库修改他们的“待办事项”应用程序http://angularjs.org/。我正在尝试使用 AJAX 将 ColdFusion CFC 链接到 Angular 应用程序。
下面是我的 Artists.cfc:
<cfcomponent>
<cffunction name="getArtists" access="remote" >
<cfargument name="firstName" default="">
<cfargument name="lastName" default="">
<cfquery name="getArtists_sql" datasource="cfartgallery">
SELECT
firstname as text,
lastname as done
FROM artists
WHERE 0=0
<cfif firstName neq "">
AND ucase(firstname) like ucase('%#FIRSTNAME#%')
</cfif>
<cfif lastName neq "">
OR ucase(lastname) like ucase('%#LASTNAME#%')
</cfif>
</cfquery>
<cfreturn getArtists_sql>
</cffunction>
</cfcomponent>
我使用 AngularJS 调用 CFC,代码如下:
function TodoCtrl($scope, $http) {
$http.get('cfc/artists.cfc?method=getArtists&returnformat=json').
success(function (response) {
$scope.todos = data.DATA;
}).
error(function (data) {
$scope.todos = data;
});
}
我知道我收到了回复。下面是 Chrome 的开发者工具为我返回的 JSON 字符串:
{
"COLUMNS":
["TEXT","DONE"],
"DATA":[
["Aiden","Donolan"],
["Austin","Weber"],
["Elicia","Kim"],
["Jeff","Baclawski"],
["Lori","Johnson"],
["Maxwell","Wilson"],
["Paul","Trani"],
["Raquel","Young"],
["Viata","Trenton"],
["Diane","Demo"],
["Anthony","Kunovic"],
["Ellery","Buntel"],
["Emma","Buntel"],
["Taylor Webb","Frazier"],
["Mike","Nimer"]
]}
这看起来不像他们演示中使用的符号 Angular:
[
{text:'learn angular', done:true},
{text:'build an angular app', done:false}
]
有人可以为我指出正确的方向,让我如何让它正常工作?理想情况下,我希望保持 CFC 完整,以便可以将其重用于不同的应用程序,因此 JSON 操作必须在 Javascript 端完成。
【问题讨论】:
-
您的问题到底是什么?顺便说一句,您的成功回调没有正确引用响应数据。应该是
function (response) { $scope.todos = response.data.DATA; }。 -
明确指出,我的问题是“如何将来自 AngularJS 的 TODO 教程的信息替换为来自 artist.cfc 的信息?我已经尝试了
function(response) {$scope.todos = response.data.DATA;}并得到了TypeError: Cannot read property 'DATA' of undefined -
我的错,我把显式的
promise.success方法(它用数据和状态参数提供回调)与promise.then方法弄错了,它用响应对象提供回调,它有一个数据属性。但在这种情况下,您的回调应该是function(data) {$scope.todos = data.DATA;},因为您的实际 ajax 响应正文确实返回了您在答案中发布的 JSON。
标签: coldfusion angularjs cfc