2015-03-08 55 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); 
     }); 
};