2015-06-24 53 views
0

我正在嘗試使用我設計的其餘api爲我的應用程序進行登錄。如果我強迫完整的URL與用戶和通它的工作原理好嗎:

http://www.myexample.com/ACTION/USER/PASSWORD/ 

但我需要從我的表單的輸入域取數據。這是函數的代碼在我的控制器:

$scope.authenticar = function(selectedUs, selectedPw){ 
    $scope.remoteData = null; 
    //alert(selectedUs); 
    dataService.getData(function(data) { 
    $scope.resultAPI = data; 
    $scope.username=$scope.resultAPI.username; 
    $scope.id_user=$scope.resultAPI.id_user; 
    $scope.isauthenticated=$scope.resultAPI.isauthenticated; 
    $scope.user_role=$scope.resultAPI.user_role; 
    $scope.complete_name=$scope.resultAPI.complete_name; 
    }); 
} 

這是服務代碼:

.service('dataService', function($http) { 
    delete $http.defaults.headers.common['X-Requested-With']; 

    this.getData = function(callback) { 
    var myparams = {a: '', u: '', p: ''}; 
    myparams.a = 'ZW50cmFy'; 
    myparams.u = 'anRk'; 
    myparams.p = '899960d8dd39b29e790845912cb35d96'; 
    $http({ 
     method: 'GET', 
     url: 'http://www.adagal.net/api_adagal/api.php', 
     withCredentials: false, 
     params: myparams, 
     headers: { 
       'Content-Type': 'application/json; charset=utf-8' 
     } 
    }).success(function(data, status, header, config){ 
    // With the data succesfully returned, call our callback 
     callback(data); 
    }).error(function(){ 
     $scope.remoteData = null; 
     return "Connection Error"; 
    }); 
    } 
}); 

我試圖通過一切方式,我怎麼能得到的URL這種方式?

+0

目前還不清楚你想在這裏做什麼。這聽起來好像你想傳遞一個動態字符串到$ http哈希url參數?真的嗎?這種情況下你的問題是什麼? –

+0

確實,我正嘗試將一個動態字符串傳遞給$ http。我的問題是,如果我將參數傳遞給函數,如下所示: this.getData = function(callback,p,u)它不工作,不傳遞數據。 –

回答

0

不完全確定你在這裏想要做什麼,你想通過用戶名和密碼d通過網址?或發佈/獲取數據?

無論哪種方式,您都需要將用戶名和密碼傳遞到服務中。您可以通過將其傳遞給服務功能來完成此操作。

this.getData = function(username, password, callback) { 
    ... 
    })... 
} 

在主叫方:

dataService.getData($scope.username, $scope.password, function(){...}); 
0

如果我理解正確你的問題,你應該改變你的服務是這樣的:

.service('dataService', function($http) { 
    delete $http.defaults.headers.common['X-Requested-With']; 
    this.getData = function(URL, callback) { 
    var myparams = {a: '', u: '', p: ''}; 
    myparams.a = 'ZW50cmFy'; 
    myparams.u = 'anRk'; 
    myparams.p = '899960d8dd39b29e790845912cb35d96'; 
    $http({ 
     method: 'GET', 
     url: URL, 
     withCredentials: false, 
     params: myparams, 
     headers: { 
       'Content-Type': 'application/json; charset=utf-8' 
     } 
    }).success(function(data, status, header, config){ 
    // With the data succesfully returned, call our callback 
     callback(data); 
    }).error(function(){ 
     $scope.remoteData = null; 
     return "Connection Error"; 
    }); 
    } 
}); 

然後調用它像這樣:

var url = "http://wwwmyexample.com/ACTION/" + $scope.selectedUs + "/" + $scope.selectedPw + "/" 
    dataService.getData(url, function(data) { 
    $scope.resultAPI = data; 
    $scope.username=$scope.resultAPI.username; 
    $scope.id_user=$scope.resultAPI.id_user; 
    $scope.isauthenticated=$scope.resultAPI.isauthenticated; 
    $scope.user_role=$scope.resultAPI.user_role; 
    $scope.complete_name=$scope.resultAPI.complete_name; 
}); 

而在HTML,你應該有水木清華這樣的:

<input ng-model="selectedUs" /> 
<input ng-model="selectedPw" /> 
0

謝謝大家的答案。我發現我的錯誤:

dataService.getData(function(u, p, data) { 

這裏是我的錯誤

dataService.getData(u, p, function(data) { 

我把U和P數據旁,不可饒恕的錯誤。謝謝大家的幫助。