AngularJS数据已成功插入但显示错误代码

时间:2015-12-31 21:54:55

标签: javascript angularjs spring-mvc

我使用Spring框架作为后端,使用角度作为前端。当我尝试从angualrJs插入数据值插入数据库但显示错误代码。请告诉我这段代码有什么不对。

var app = angular.module("categoryApp", []);
app.controller('submitCategory', [ '$scope', '$http',
    function($scope, $http) {
        $scope.submitClick = function() {
            var dataObj = {
                name : $scope.name
            };
            var result = $http.post("/tutorials/category", dataObj);
            result.success(function(data, status, headers, config) {
                alert("success");
                $scope.message = data;
            });
            result.error(function(data, status, headers, config) {
                alert("failure message: " + JSON.stringify({
                    data : data
                }));
            });

            $scope.name = '';
        }
    }
]);

我的HTML是

<body ng-app="categoryApp">
    <section class="panel" ng-controller="submitCategory">
        <header class="panel-heading"> Basic Forms </header>
        <div class="panel-body">
            <form role="form" method="post" ng-submit="submitClick()">
                <div class="form-group">
                    <label for="category">Category</label> <input type="text"
                        class="form-control" id="exampleInputEmail1" name="name"
                        placeholder="Category" ng-model="name">
                </div>
                <button type="submit" class="btn btn-primary">Submit</button>
            </form>

        </div>
    </section>
</body>

我的控制器方法是

@RequestMapping(value = "/category", method = RequestMethod.POST)
    public ResponseEntity<String> postCategory(@RequestBody Category category,
            Model model) {
        categoryService.save(category);
        return new ResponseEntity<String>("success", HttpStatus.OK);
    }

1 个答案:

答案 0 :(得分:0)

问题是由ResponseEntity&lt; String &gt;引起的。你的代码java应该是这样的:

@RequestMapping(value = "/category", method = RequestMethod.POST) 
    public ResponseEntity<String> postCategory(@RequestBody Category category, 
            Model model) { 
    categoryService.save(category); 
    return new ResponseEntity<String>("success", HttpStatus.OK); }