AngularJS - 错误正文请求 POST
AngularJS - Error Body Request POST
我是 AngularJs 的新手,我需要使用枚举形式在服务器上执行 post 请求。我在 JS 中这样做:
function completeTaskAction2($scope, $http, Base64) {
$http.defaults.headers.common['Authorization'] = 'Basic ' + Base64.encode('kermit' + ':' + 'kermit');
$http.get("http://localhost:8080/activiti-rest/service/runtime/tasks")
.then(function (response, data, status, headers, config) {
var ids = response.data.data[0].id;
$scope.formData2 = {
taskId: ids,
properties: [{
id: 'requestApproval',
value: ''
}
]
}
});
$scope.submitForm2 = function () {
$http({
method: 'POST',
url: "http://localhost:8080/activiti-rest/service/form/form-data",
data: angular.toJson($scope.formData2),
headers: {
'Authorization': 'Basic ' + Base64.encode('kermit' + ':' + 'kermit'),
'Accept': 'application/json',
'Content-Type': 'application/json'
}
}).success(function (data) {
console.log("OK", data);
}).error(function (data) {
console.log("Error Posting data...");
console.log(data);
console.log($scope.submitForm2);
});
};
};
并在 html 中:
<div ng-controller="completeTaskAction2">
<div ng-repeat="x in names">
{{ x.name }}*
<form ng-submit="submitForm2()">
<a ng-if="x.type=='enum'">
<select ng-model="formData2.properties[0].value" ng-options="y.name for y in x.enumValues " placeholder="{{ x.name[0] }}"> {{ x.name }} </select>
</a>
<br>
<button type="submit" class="btn btn-success btn-lg btn-block">
<span class="glyphicon glyphicon-flash"></span> Submit Enum!
</button>
</div>
</div>
</form>
但是我有这个错误:
Could not read JSON: Can not deserialize instance …
响应正文是:
{"taskId":"67762","properties":[{"id":"requestApproval","value":{"id":"true","name":"Yes"}}]}
我只需要将这个发送到服务器:
**{"taskId":"67762","properties":[{"id":"requestApproval","value":"true"}}]**
我该怎么做?
而不是这个:
<select ng-model="formData2.properties[0].value" ng-options="y.name for y in x.enumValues " placeholder="{{ x.name[0] }}"> {{ x.name }} </select>
您应该像这样使用 ng-selected:
<select ng-model="formData2.properties[0].value" ng-options="y.name for y in x.enumValues " ng-selected="x.name[0]"></select>
这应该可以解决您的问题,但遗憾的是,我现在无法对此进行测试。让我知道这是否能解决您的问题:)
你的ng-options应该是这样的:
ng-options="y.id as y.name for y in x.enumValues"
这会将 y
的 ID 绑定到模型值而不是整个 y
对象。
我是 AngularJs 的新手,我需要使用枚举形式在服务器上执行 post 请求。我在 JS 中这样做:
function completeTaskAction2($scope, $http, Base64) {
$http.defaults.headers.common['Authorization'] = 'Basic ' + Base64.encode('kermit' + ':' + 'kermit');
$http.get("http://localhost:8080/activiti-rest/service/runtime/tasks")
.then(function (response, data, status, headers, config) {
var ids = response.data.data[0].id;
$scope.formData2 = {
taskId: ids,
properties: [{
id: 'requestApproval',
value: ''
}
]
}
});
$scope.submitForm2 = function () {
$http({
method: 'POST',
url: "http://localhost:8080/activiti-rest/service/form/form-data",
data: angular.toJson($scope.formData2),
headers: {
'Authorization': 'Basic ' + Base64.encode('kermit' + ':' + 'kermit'),
'Accept': 'application/json',
'Content-Type': 'application/json'
}
}).success(function (data) {
console.log("OK", data);
}).error(function (data) {
console.log("Error Posting data...");
console.log(data);
console.log($scope.submitForm2);
});
};
};
并在 html 中:
<div ng-controller="completeTaskAction2">
<div ng-repeat="x in names">
{{ x.name }}*
<form ng-submit="submitForm2()">
<a ng-if="x.type=='enum'">
<select ng-model="formData2.properties[0].value" ng-options="y.name for y in x.enumValues " placeholder="{{ x.name[0] }}"> {{ x.name }} </select>
</a>
<br>
<button type="submit" class="btn btn-success btn-lg btn-block">
<span class="glyphicon glyphicon-flash"></span> Submit Enum!
</button>
</div>
</div>
</form>
但是我有这个错误:
Could not read JSON: Can not deserialize instance …
响应正文是:
{"taskId":"67762","properties":[{"id":"requestApproval","value":{"id":"true","name":"Yes"}}]}
我只需要将这个发送到服务器:
**{"taskId":"67762","properties":[{"id":"requestApproval","value":"true"}}]**
我该怎么做?
而不是这个:
<select ng-model="formData2.properties[0].value" ng-options="y.name for y in x.enumValues " placeholder="{{ x.name[0] }}"> {{ x.name }} </select>
您应该像这样使用 ng-selected:
<select ng-model="formData2.properties[0].value" ng-options="y.name for y in x.enumValues " ng-selected="x.name[0]"></select>
这应该可以解决您的问题,但遗憾的是,我现在无法对此进行测试。让我知道这是否能解决您的问题:)
你的ng-options应该是这样的:
ng-options="y.id as y.name for y in x.enumValues"
这会将 y
的 ID 绑定到模型值而不是整个 y
对象。