如何使用角度 js 通过 Web api 将对象数组传递到列表<>
How to pass array of object through web api to a List<> using angular js
这是我在控制器中的数组:
$scope.arr = [];
$scope.arr.push({
QuestionId1: firstQuestionId,
QuestionId2: secondQuestionId,
SecurityAnswer1: firstQuestionAnswer,
SecurityAnswer2: secondQuestionAnswer
});
AccountService.SubmitSecurityAnswer($scope);
那么这是我在 Angular 服务中的 API:
fact.SubmitSecurityAnswer = function (d) {
debugger;
return $http({
method: 'POST',
url: API_RESOURCES.API_Domain + 'api/Account/SaveSecurityAnswers',
headers: { 'content-Type': 'application/x-www-form-urlencoded' },
transformRequest: function (obj) {
var str = [];
for (var p in obj)
str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
return str.join("&");
},
isArray: true,
data: d.arr
}).success(function (response) {
return response;
此 api 调用的 C# 函数是:
public HttpResponseMessage SaveSecurityAnswers(List<SecurityQuestion_CustomerMap> obj)
{
当我通过断点检查"obj"时,它显示 count=0..我认为"data: d.arr"的格式不正确还是不正确?请帮忙。
在客户端代码中进行以下更改以将请求作为 json 传递。我假设对象列表被添加到您要传入的另一个对象 (d) 中?如果没有,那么在 JSON.stringify 中删除 .arr,如果它是对象列表,只需发送"d"。
fact.SubmitSecurityAnswer = function (d) {
return $http({
url: API_RESOURCES.API_Domain + 'api/Account/SaveSecurityAnswers',
dataType: 'json',
method: 'POST',
data: JSON.stringify(d.arr),
headers: {
"Content-Type": "application/json"
}
});
}
并更改终结点以在请求正文中查找对象列表。
[HttpPost]
public HttpResponseMessage SaveSecurityAnswers([FromBody]List<SecurityQuestion_CustomerMap> obj)
相关文章:
- 用每小时的差值填充数组/列表-从下拉列表中给定两个时间值
- 使用图像数组列表的simplegallary.js显示适合桌面屏幕的图像
- 从多维嵌套json数组创建下拉列表
- 如何发送分配列表<字符串>到JavaScript数组或可枚举对象
- Jquery 读取编号组中选择下拉列表的数组
- 如何从 URL 数组向文档添加图像列表
- 为对象数组创建列表项
- 从javascript中的数组列表中获取值
- 从select创建项目列表/数组
- 列表/数组的迭代问题
- 列表/数组是否有效 JSON
- 如何在C# MVC的VIew中将列表数组拆分为单独的数组
- 对列表/数组中的每个项目运行相同的函数
- 需要 JQuery 标记从数据库的列表/数组中选中的复选框
- javascript indexOf - 列表/数组中的完全匹配
- PHP 从现有代码中随机化文件列表数组
- 使用自定义函数从字典中创建列表/数组
- 检查元素是否具有在列表/数组中给定的类
- html中的下拉列表数组
- 如何在javascript中读取对象文件列表数组