gpt4 book ai didi

angularjs - 如何做http post json数据

转载 作者:行者123 更新时间:2023-12-05 00:55:06 33 4
gpt4 key购买 nike

我是编程和 Angular 的新手。
我的 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"
});
});

这是我为 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 }

最佳答案

尝试这个

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);
}

关于angularjs - 如何做http post json数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38760145/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com