Angular JS - $ http获取的动态URL

时间:2015-06-24 17:16:49

标签: javascript angularjs

我正在尝试使用我设计的rest 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";
    });
  }
});

我试图通过所有方式,如何以这种方式获取网址?

3 个答案:

答案 0 :(得分: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" />

答案 1 :(得分:0)

不完全确定您在此处尝试做什么,您想通过网址传递用户名和密码?或发布/获取数据?

无论哪种方式,您都需要将用户名和密码传递给服务。你可以通过在服务功能中传递它来做到这一点。

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

在主叫方:

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

答案 2 :(得分:0)

谢谢大家的答案。我发现了我的错误:

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

这是我的错误

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

我把你和p放在数据旁边,不可原谅的错误。谢谢大家的帮助。