2016-02-05 64 views
0

我试图从我的离子应用程序发布数据到服务器。要使用的URL位于一个变量中。这里是我的尝试:使用AngularJS(离子框架)将数据POST到服务器

首先我创建了一个服务是这样的:

.service('PostTuto', ['$http',function($http) { 
    this.servicioPostTuto = function(tema,ubicacion,horario,nom_coe) { 
     return $http({ 
       method: 'POST', 
       url: 'http://"""""".""".""""."""":8080/smartlandiotv2/webresources/entidades.datos/insert?apikey=3bff8615827f32442199fdb2ed4df4&trama={"Nombre":"'+tema+'","Apellido":"'+ubicacion+'","Sexo":"'+horario+'","Residencia":"'+nom_coe+'"}', 
      }); 
    }; 
}]) 
在这个部分我在URL发送变量 temaubicacionhorarionom_coe和我的控制器

。我已经做到了这一点:

$scope.data = {}; 
    $scope.create=function(){ 
    $scope.nom_coe="IDfromSubject" 
    PostTuto.servicioPostTuto($scope.data.tema,$scope.data.ubicacion,$scope.data.horario,$scope.nom_coe) 
    .success(function(data){ 
     var alertPopup = $ionicPopup.alert({ 
     title: 'created' 
     }); 
    }) 
    .error(function(data){ 
     var alertPopup = $ionicPopup.alert({ 
     title: 'Error to post data' 
     }); 
    }); 
    }; 
    //FIN POSTEAR DATOS A SERVIDOR 

在这一部分,我收到我的html文件,从输入的数据。

<label class="item item-input item-stacked-label"> 
      <span class="input-label"><strong>Tema:</strong></span> 
      <input type="text" ng-model="data.tema" ng-disabled="editar"> 
     </label> 
     <label class="item item-input item-stacked-label"> 
      <span class="input-label"><strong>Ubicacion:</strong></span> 
      <input type="text" ng-model="data.ubicacion" ng-disabled="editar"> 
     </label> 
     <label class="item item-input item-stacked-label"> 
      <span class="input-label"><strong>Horario:</strong></span> 
      <input type="text" ng-model="data.horario" ng-disabled="editar"> 
     </label> 
     <a class="button button-block icon-left ion-ios-compose button-positive" 
      ng-disabled="!data.tema || !data.ubicacion || !data.horario" ng-click="create()"> 
      create 
     </a> 

当我尝试发布的数据,我的应用程序返回我一个错误“错误发布数据”,我该怎么解决呢?

+0

你可以检查你的webservice邮递员它是否工作。 –

回答

3

发送查询字符串中的API密钥是不是一个好way.You可以试试这个

在控制器的js

$scope.data=new Object(); 

$scope.create=function(){ 
PostTuto.servicioPostTuto($scope.data) 
.success(function(data){ 
    var alertPopup = $ionicPopup.alert({ 
    title: 'created' 
    }); 
}) 
.error(function(data){ 
    var alertPopup = $ionicPopup.alert({ 
    title: 'Error to post data' 
    }); 
}); 
}; 

而在你AppConfig.js

app.run(function($http){ 
      $http.defaults.headers.common['APIKey'] = "3bff8615827f32442199fdb2ed4df4"; 
}) 

而在你的服务

.service('PostTuto', ['$http',function($http) { 
    this.servicioPostTuto = function(data) { 
    return $http.post('http://"""""".""".""""."""":8080/smartlandiotv2/webresources/entidades.datos/insert',data); 
    } 

}]) 

现在从服务器端,你可以从请求头为你的目的得到你的APIKey

相关问题