2015-03-08 77 views
0

我不明白$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":"[email protected]"}]" 

这是工厂:

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

可能重复[如何返回从异步响应打电话?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – charlietfl 2015-03-08 22:16:27

回答

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); 
     }); 
};