Reputation: 155
i am new in programming and angular. i have the data like this in my controller
temanService.create({
Userid: $scope.datateman.userid,
Address: $scope.datateman.address,
Employeeid: $scope.datateman.employeeid,
Email: $scope.datateman.email,
Phoneno: $scope.datateman.phoneno,
Password: $scope.datateman.password,
Division: $scope.datateman.division,
Leavebalance: $scope.datateman.leavebalance
}).success(function(data) {
$scope.showAlert({
title: "Information",
message: "Data Telah Tersimpan"
});
});
and this is my service for http.request
angular.module('starter.services', [])
.factory('temanService', function($http) {
var baseUrl = 'http://localhost:60820/MobileBootcamp.svc/';
return {
create: function(datateman) {
return $http.post(baseUrl + 'insert?method=insertuser',
datateman, // <--- what to insert here
{
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
});
},
update: function(datateman) {
return $http.post(baseUrl + 'update.php', datateman, {
headers: {
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8;'
}
});
},
delete: function(id) {
return $http.get(baseUrl + 'delete.php?id=' + id);
}
};
});
and this is example of the correct parameter for insert
{
"Value": "userid|address|employeeid|email|phoneno|password|division|leavebalanc"
}
My question is,
How to put data to the http request post method?
i wanted to make like this
method: 'POST',
url: 'http://110.35.82.163:9090/MobileBootcamp.svc/insert? method=insertnews',
crossDomain: true,
headers: {
'Content-Type': 'application/json; charset=utf-8'
},
data: { "Value": userID + "|" + $scope.title + "|" + $scope.posttag + "|" + $scope.content }
Upvotes: 4
Views: 20532
Reputation: 55248
Try this
var data = {
Userid: $scope.datateman.userid,
Address: $scope.datateman.address,
Employeeid: $scope.datateman.employeeid,
Email: $scope.datateman.email,
Phoneno: $scope.datateman.phoneno,
Password: $scope.datateman.password,
Division: $scope.datateman.division,
Leavebalance: $scope.datateman.leavebalance
};
var dataCollection = [];
for(var item in data){
if(data.hasOwnProperty(item)){
dataCollection.push(data[item]);
}
}
var DTO = {
Value: dataCollection.join('|');
};
//do not use success. its obsolete
temanService.create(DTO).then(function(response){
},function(response){
//error
});
Writing service like this will be more readable.
create: function(datateman) {
var config = {
method: "POST",
url: baseUrl + 'insert?method=insertuser',
data: datateman,
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
};
return $http(config);
}
Upvotes: 3
Reputation: 68685
This is the documentation
$http.post('/someUrl', data, config).then(successCallback, errorCallback);
In your code you can do
var data = { "Value": datateman.userID + "|" + $scope.title + "|" + $scope.posttag + "|" + $scope.content } + 'your next values'.
$http.post(baseUrl+'insert?method=insertuser', JSON.stringify(data) ,{
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
});
Upvotes: 1
Reputation: 77
Just add data attribute to your service.
For your code add like the following
create: function (datateman){
return $http.post(baseUrl+'insert?method=insertuser',datateman,{
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
});
Upvotes: 1