我正在尝试从我的离子应用程序将数据发布到服务器。要使用的URL位于变量中。这是我试过的:
首先我创建了这样的服务:
.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+'"}',
});
};
}])
在这部分中,我将变量tema
,ubicacion
,horario
,nom_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 :(得分:3)
在查询字符串中发送API密钥不是一个好方法。您可以尝试这个
在Controller 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);
}
}])
现在从服务器端,您可以从Request标头获取APIKey