我不明白$http
服务是如何运作的。我有一个调用工厂的控制器,工厂调用WS函数。
如果我在调试模式下运行应用程序,工厂会调用WS函数,而WS函数会返回我认为是json对象的内容。
另一方面,当我在Web控制台中观看时,收到消息“undefined”。 什么应该返回WS功能?或者我的工厂出了什么问题?
提前致谢。
这是VB.Net Web服务功能:
<ScriptMethod(ResponseFormat:=ResponseFormat.Json, XmlSerializeString:=False)> _
<WebMethod()> _
Public Function getLogin(ByVal email As String, ByVal password As String) As String
Dim dt As New Data.DataTable
Dim result As String = ""
Using con As New SqlConnection(sConn)
'Dim sql As String = "Select id_usuario, nombre_usuario id_tipo_usuario from binder_usuarios where email = @email and password = @password"
Dim sql As String = "Select * from binder_usuarios where email = @email and password = @password"
Dim cmd As New SqlCommand
cmd.CommandText = sql
cmd.Connection = con
cmd.Parameters.AddWithValue("@email", email)
cmd.Parameters.AddWithValue("@password", password)
Dim dataAdapter As New SqlDataAdapter(cmd)
dataAdapter.Fill(dt)
If dt.Rows.Count > 0 Then
result = JsonConvert.SerializeObject(dt, New JavaScriptDateTimeConverter())
Return result
Else
result = "e"
End If
Return result
End Using
End Function
WS函数返回一个字符串:
"[{"id_usuario":3,"nombre_usuario":"Quethzel","apellido_paterno":"Diaz","apellido_materno":"Zarate","id_area":1,"id_tipo_usuario":1,"password":"sa","email":"hqdiaz@lis.com.mx"}]"
这是工厂:
function fcQAuth($http, $q, $window) {
return {
loginAuth: function(credentials) {
$http.post('../ws/wsReact.asmx/getLogin', credentials)
.success(function(data, status) {
return "your data it's Ok, in da face !";
})
.error(function(data, status) {
return "don't give up Nigga";
});
//return "myUser. " + credentials.email + ", myPass." + credentials.password;
}
}
}; // end fcQAuth factory
这是控制器,它叫工厂:
function loginCtrl($scope, $q, $http, $location, fcQAuth) {
$scope.loginUser = function() {
var credentials = {
email: $scope.user.email === '' || $scope.user.email === undefined ? 0 : $scope.user.email,
password: $scope.user.password === '' || $scope.user.password === undefined ? 0 : $scope.user.password
};
$scope.userInfo = fcQAuth.loginAuth(credentials);
console.log($scope.userInfo);
};
};
答案 0 :(得分:0)
http.post是一个异步调用,当执行控制器中的console.log时,http.post还没有返回。试试这个:
返回工厂的承诺:
function fcQAuth($http, $q, $window) {
return {
loginAuth: function(credentials) {
return $http.post('../ws/wsReact.asmx/getLogin', credentials);
}
};
}
获取控制器中成功回调的数据:
function loginCtrl($scope, $q, $http, $location, fcQAuth) {
var credentials = {
email: $scope.user.email === '' || $scope.user.email === undefined ? 0 : $scope.user.email,
password: $scope.user.password === '' || $scope.user.password === undefined ? 0 : $scope.user.password
};
fcQAuth.loginAuth(credentials).then(function(data){
$scope.userInfo = data;
console.log($scope.userInfo);
});
};