我是编程和角色的新手。 我在我的控制器中有这样的数据
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"
});
});
这是我的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);
}
};
});
这是插入
的正确参数的示例{
"Value": "userid|address|employeeid|email|phoneno|password|division|leavebalanc"
}
我的问题是,
如何将数据放入http请求post方法?
我想这样做
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 }
答案 0 :(得分:2)
试试这个
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
});
这样的写作服务将更具可读性。
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);
}
答案 1 :(得分:1)
这是文档
$http.post('/someUrl', data, config).then(successCallback, errorCallback);
在您的代码中,您可以
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'
}
});
答案 2 :(得分:1)
只需将数据属性添加到您的服务中即可。
为您的代码添加如下
create: function (datateman){
return $http.post(baseUrl+'insert?method=insertuser',datateman,{
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
});