下面是我从后端生成的json数据
{"langs":[{"cisAreaId":100,"area":"Prog","name":"C#"},{"cisAreaId":110,"area":"Prog","name":"Java"},{"cisAreaId":120,"area":"Prog","name":"MS.NET languages(VB.NET,etc)"},{"cisAreaId":130,"area":"Prog","name":"MS Visual Languages (VB, C++, etc)"},{"cisAreaId":140,"area":"Prog","name":"Python"},{"cisAreaId":150,"area":"Prog","name":"Ruby"}]}
以上数据我复制到$ scope.areas。 现在在我看来
<div ng-repeat="lang in areas.langs">
<label><input type="checkbox" ng-model="lang.selected" value="{{lang.cisAreaId}}" /> {{lang.name}}</label>
</div>
用户点击提交后,我需要捕获所选的复选框值并将其作为JSON数据发送。
$scope.languageArray = [];
angular.forEach($scope.areas.langs, function (lang) {
if (lang.selected) $scope.languageArray.push({ "cisAreaId": lang.cisAreaId });
});
var data = JSON.stringify({
languages: $scope.languageArray,
});
在选择两个复选框值
之后形成数组{"languages":[{"cisAreaId":110},{"cisAreaId":120}]}
从上面的代码中我如何在URL中传递上述动态数组来调用后端方法
Angularjs Controller Code:
XXXService.step2Form(data).then(function (results) {
console.log(results.data);
}, function (error) {
alert(error.data.message);
});
service code:
var _step2Form = function (data) {
return $http.post(serviceBase + 'api/feedback/Step2Data' + data , { headers: { 'Content-Type': 'application/json' } });
};
我已将数据动态传递到后端
public HttpResponseMessage Step2Data([FromBody] int[] languages)
但是我收到了这样的错误
http://localhost:53401/api/XXXX/Step2Data[object%20Object],[object%20Object] 404 (Not Found)
任何人都可以告诉我如何在url中动态传递数组值来调用webapi方法。
答案 0 :(得分:2)
$scope.languageArray
中你只需要推送id:
$scope.languageArray = [];
angular.forEach($scope.areas.langs, function (lang) {
if (lang.selected) $scope.languageArray.push(lang.cisAreaId);
});
var data = JSON.stringify({
languages: $scope.languageArray,
});