简体   繁体   English

AngularJS-错误正文请求POST

[英]AngularJS - Error Body Request POST

I'm newbie to AngularJs, i need to do a post request on a server with enum form. 我是AngularJs的新手,我需要使用枚举形式在服务器上进行发布请求。 I do this in this way in JS: 我在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);
    });
};

}; };

and in html: 并在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>

but i have this error: 但是我有这个错误:

Could not read JSON: Can not deserialize instance … 无法读取JSON:无法反序列化实例…

and response body is: 响应主体为:

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

I need to send only this to the server: 我只需要将此发送到服务器:

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

How i can do this? 我该怎么做?

Instead of this: 代替这个:

<select ng-model="formData2.properties[0].value" ng-options="y.name for y in x.enumValues " placeholder="{{ x.name[0] }}"> {{ x.name }} </select>

You should use ng-selected like so: 您应该像这样使用ng-selected:

<select ng-model="formData2.properties[0].value" ng-options="y.name for y in x.enumValues " ng-selected="x.name[0]"></select> 

This should solve your problem but I can't test this right now unfortunately. 这应该可以解决您的问题,但是很遗憾,现在无法测试。 Let me know if this solves your issue :) 让我知道这是否解决了您的问题:)

Your ng-options should be like this: 您的ng-options应该像这样:

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

this will bind id of y to the model value instead of whole y object. 这会将y ID绑定到模型值,而不是整个y对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM