AngularJS - 错误正文请求POST

时间:2016-01-07 11:43:24

标签: javascript angularjs forms post activiti

我是AngularJs的新手,我需要在具有枚举形式的服务器上发布帖子请求。我在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>

但我有这个错误:

  

无法读取JSON:无法反序列化实例...

和响应正文是:

{"taskId":"67762","properties":[{"id":"requestApproval","value":{"id":"true","name":"Yes"}}]}

我只需要将它发送到服务器:

**{"taskId":"67762","properties":[{"id":"requestApproval","value":"true"}}]**

我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

而不是:

<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> 

这应该可以解决您的问题,但我现在无法对此进行测试。如果这解决了您的问题,请告诉我:))

答案 1 :(得分:1)

你的ng选项应该是这样的:

ng-options="y.id as y.name for y in x.enumValues"

这会将y的ID绑定到模型值,而不是整个y对象。