2015-12-31 52 views
1

我用Spring框架作爲後端和角度作爲前端。當我嘗試從angualrJs插入數據值插入數據庫,但顯示錯誤代碼。請告訴我這段代碼有什麼問題。Angualr Js數據插入成功,但它顯示錯誤代碼

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); 
    } 
+0

你問題是與數據綁定,你可以告訴你查看代碼(html) – aitnasser

+0

aitnasser我把我的看法部分也可以請你看看它一次。 – sudar

+0

我沒有看到你想顯示你的數據來自服務器 – aitnasser

回答

0

問題由ResponseEntity < 字符串>。你的代碼的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); } 
相關問題