我正在努力学习一些或多或少的基础知识......关于在我的WebApi2 SPA应用程序中调用休息服务。
所以..基本上我正在尝试做的事情(首先作为一个例子),用两个字符串调用服务:用户名和密码。
这是WebService(asp.net中的UserController.cs)
public HttpResponseMessage Get(string username, string password)
{
var authUser = ControllerBasePage.UserServices.AuthenticateUser(username, password);
if (authUser != null)
{
return Request.CreateResponse(HttpStatusCode.OK, authUser);
}
throw new HttpResponseException(HttpStatusCode.NotFound);
}
以下是角度工厂和用法:
angular.module('photoAppLogin')
.factory('dataFactory', ['$http', function ($http) {
var urlBase = 'api/';
var dataFactory = {};
dataFactory.getUser = function (username, password) {
var config = {
url: urlBase + "user",
params: { username: username, password: password }
};
//return $.post(urlBase + "user", { username: username, password: password });
return $.ajax.get(config);
};
return dataFactory;
}]);
并且......控制器动作:
angular.module("photoAppLogin").controller('mainController',
function ($scope, dataFactory) {
$scope.loggedInUser;
$scope.status;
// create a message to display in our view
$scope.message = 'Loginform here !';
$scope.LoginUser = function () {
var uname = $scope.Username;
var pw = $scope.Pass;
dataFactory.getUser(uname, pw)
.success(function(usr) {
$scope.loggedInUser = usr;
$.cookie('loggedInUser', uname);
})
.error(function(error) {
$scope.status = "User not found" + error.message;
});
}
});
我能够获得一个空的GET服务,但我无法获得具有两个参数的服务。我已经尝试了两个帖子和获取(!!!)但似乎无法直接得到这个......
这是我得到的(唯一)错误:
TypeError: undefined is not a function
at Object.dataFactory.getUser (<http-localhost-here>/Factories/dataFactory.js:14:23)
at Scope.$scope.LoginUser (<http-localhost-here>/loginscripts.js:36:21)
at Parser.functionCall (<http-localhost-here>/Scripts/angular.js:10567:21)
at <http-localhost-here>/Scripts/angular.js:18627:17
at Scope.$get.Scope.$eval (<http-localhost-here>/Scripts/angular.js:12412:28)
at Scope.$get.Scope.$apply (<http-localhost-here>/Scripts/angular.js:12510:23)
at HTMLButtonElement.<anonymous> (<http-localhost-here>/Scripts/angular.js:18626:21)
at HTMLButtonElement.n.event.dispatch (<http-localhost-here>/Scripts/jquery-2.1.1.min.js:3:6404)
at HTMLButtonElement.n.event.add.r.handle (<http-localhost-here>/Scripts/jquery-2.1.1.min.js:3:3179) angular.js:9778