使用angular/javascript使用WCF Web服务
我正在用WCF实现一个服务器,并尝试使用Angular开发的客户机访问其rest服务 服务内容如下:使用angular/javascript使用WCF Web服务,javascript,angularjs,web-services,wcf,Javascript,Angularjs,Web Services,Wcf,我正在用WCF实现一个服务器,并尝试使用Angular开发的客户机访问其rest服务 服务内容如下: public interface IConnexion { [OperationContract] [WebGet(UriTemplate = "Connexion/{login}/{mdp}", ResponseFormat = WebMessageFormat.Json)] Utilisateur Connexion(string login, string mdp); [Opera
public interface IConnexion
{
[OperationContract]
[WebGet(UriTemplate = "Connexion/{login}/{mdp}", ResponseFormat = WebMessageFormat.Json)]
Utilisateur Connexion(string login, string mdp);
[OperationContract]
[WebInvoke(UriTemplate = "Deconnexion/{id_user}", RequestFormat = WebMessageFormat.Json)]
void Deconnexion(string id_user);
app.service('serverConnexionServices', function(userService, serverConfigService) {
this.Connexion = function(login, pass)
{
var uri = this.getServerUri() + "Connexion/"+login+"/"+CryptoJS.MD5(pass);
var promises = $http.get(uri)
.success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
return data;
}).
error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
return "";
});
return promises;
};
});
}
我的职能部门正在尝试接触服务:
public interface IConnexion
{
[OperationContract]
[WebGet(UriTemplate = "Connexion/{login}/{mdp}", ResponseFormat = WebMessageFormat.Json)]
Utilisateur Connexion(string login, string mdp);
[OperationContract]
[WebInvoke(UriTemplate = "Deconnexion/{id_user}", RequestFormat = WebMessageFormat.Json)]
void Deconnexion(string id_user);
app.service('serverConnexionServices', function(userService, serverConfigService) {
this.Connexion = function(login, pass)
{
var uri = this.getServerUri() + "Connexion/"+login+"/"+CryptoJS.MD5(pass);
var promises = $http.get(uri)
.success(function(data, status, headers, config) {
// this callback will be called asynchronously
// when the response is available
return data;
}).
error(function(data, status, headers, config) {
// called asynchronously if an error occurs
// or server returns response with an error status.
return "";
});
return promises;
};
});
我的窗体控制器:
app.directive('loginContent', function(userService, serverConnexionServices){
return{
restrict : 'E',
templateUrl : "includes/home/login-content.html",
controllerAs: "loginController",
controller: function($scope, userService, serverConnexionServices) {
var IsSubmitEnabled = true;
this.errors = "";
this.validateLogin = function(user) { // Calling the service
this.IsSubmitEnabled = false; // To avoid more than one click while checking on the server
var retour = serverConnexionServices.TryConnection(user).then(function(promise) {
alert('promise:'+retour);
if(promise.length > 0)
{ // There is an error so we show a message to the user
this.promise= retour;
}
this.IsSubmitEnabled = true;
user = {};
});
};
}
}
});
但是,当我尝试打开客户端时,在Visual Studio中引发断点之前会出现警报框(我将断点放在“Connexion”函数的第一行上。因此,这意味着服务器尚未响应请求,但$http.get()已发送其答案。因此结果对象不包含服务器正在发送的内容。根据Angular的文档,响应将异步发送。但是如何强制请求等待答案
我的控制器现在从服务器获得答案,但它不会更新视图,如果我尝试手动调用$scope.$apply(),我会得到错误“$digest ready in progress”。我如何强制视图刷新自身?(我注意到,如果我将此设置为.errors=“aaa”;一旦“then”关闭,我的视图将刷新,谁是“aaa”,我还检查了errors.length>0,并显示了一条警报消息)
我的看法是:
<div ng-show="loginController.errors.length > 0" class="alert alert-danger" role="alert" style="display: inline-block;"><span class="glyphicon glyphicon-exclamation-sign" aria-hidden="true"></span>{{loginController.errors}}</div>
{{loginController.errors}
EDIT:我现在可以在Visual Studio中实现Connexion
时到达断点,但服务器的答案仍然不是预期的结果
EDIT2:我添加了需要刷新的控制器
EDIT3:好的,我更新了我的代码,我需要在控制器的调用中加入一个“then”函数,我的控制器现在得到了答案。但是,我的视图不会在出现错误时自动更新。它应该将div可见性设置为true,但异步调用似乎会阻止它。我尝试使用apply()和digest()没有成功。你能在这方面帮助我吗?你的代码看起来更像jQuery而不是AngularJS。在AngularJS中,你可以使用该服务进行AJAX调用。好的……我遵循了另一个教程,它似乎更好(我现在可以在“COnnexion”的实现中达到断点)在Visual Studio中,但服务器给出的答案仍然不是预期的。您可能正在朝着正确的方向前进,当前的问题可能与范围相关。我认为这里提供的客户端代码并不代表所有相关部分。您可以使用plunk或JSFIDLE,或者至少向我们指出您开发的URL吗nt代码可以在运行中看到吗?在尝试使用moody服务时,一些一般性建议:首先使用类似的方法让服务调用正常工作。然后尝试使用angular/jQuery之类的工具进行操作。最后,您所要做的就是使用浏览器开发工具(F12)中的“网络”选项卡来比较网络上的实际通信。我对此不完全确定,但我可以想象WCF并不是那么容易使用。也许你缺少了一些标题或类似的内容。谢谢!只需要放入$scope.promise,我的服务运行良好,我的UI刷新!