2017-07-06 70 views
1

我正在使用前端和J2EE後端使用Angular的應用程序,我做了一個表格,我必須發佈數據將被保存在數據庫中
問題,後工作正常,但我不能得到服務器在添加後的反應,我總是得到這個錯誤(奇怪的錯誤進來灰色不紅,通常情況下)如何解決http baddata角度錯誤?

error screenshot

Error: [$http:baddata] http://errors.angularjs.org/1.6.4/$http/baddata?p0=%7B%22success%22%7D&p1=%7B%7D at angular.min.js:6 at nc (angular.min.js:96) at angular.min.js:97 at q (angular.min.js:7) at xd (angular.min.js:97) at f (angular.min.js:99) at angular.min.js:134 at m.$digest (angular.min.js:145) at m.$apply (angular.min.js:149) at l (angular.min.js:102)

這裏的角碼

$scope.wflow = { 
    "worcode": "HELLOoo", 
    "wordest": "AVDOSS", 
    "worstatus": "ACTIF", 
    "worheight": 0, 
    "lancode": "EN", 
    "worlabel": "Salut monde", 
    "wordescription": "Salut monde", 
    "size": 0 
}; 
$scope.submitForm = function() { 
    console.log(JSON.stringify($scope.wflow)); 

    $http({ 
     method: 'POST', 
     headers: { 
      'Content-Type': 'application/json' 
     }, 
     url: host + 'api/workflow/add', 
     data: $scope.wflow 
    }).then(function (response) { 
     console.log(response); 
    }, function (response) { 
     console.log(response); 
    }); 

}; 

而這裏的Java的一個

@RequestMapping(value = "/add", method = RequestMethod.POST) 
@ResponseBody 
public ResponseEntity<String> addWorkflow(@RequestBody LWflow lworkflow){ 
    service.addWorkflow(lworkflow); 
    return new ResponseEntity<String>("{\"success\"}", HttpStatus.OK); 
} 

是,如果需要

 <table class="table"> 
     <tbody> 
     <tr> 
     <td><b>Code</b></td> 
     <td><input type="text" name="worcode" class="form-control" ng-model="wflow.worcode"></td> 
     </tr> 
     <tr> 
     <td><b>Destination</b></td> 
     <td><input type="text" name="wordest" class="form-control" ng-model="wflow.wordest"><td> 
     </tr> 
     <tr> 
     <td><b>Status</b></td> 
     <td><input type="text" name="worstatus" class="form-control" ng-model="wflow.worstatus"></td> 
     </tr> 
     <tr> 
     <td><b>Height</b></td> 
     <td><input type="number" name="worheight" class="form-control" ng-model="wflow.worheight"><td> 
     </tr> 
     <tr> 
     <td><b>Langue</b></td> 
     <td><input type="text" name="lancode" class="form-control" ng-model="wflow.lancode"></td> 
     </tr> 
     <tr> 
     <td><b>Label</b></td> 
     <td><input type="text" name="worlabel" class="form-control" ng-model="wflow.worlabel"></td> 
     </tr> 
     <tr> 
     <td><b>Description</b></td> 
     <td><input type="text" name="wordescription" class="form-control" ng-model="wflow.wordescription"></td> 
     </tr> 
     <tr> 
     <td><b>Taille</b></td> 
     <td><input type="number" name="size" class="form-control" ng-model="wflow.size"></td> 
     </tr> 
     </tbody> 
     </table> 

     </div> 
     <div class="modal-footer"> 
     <button type="button" class="btn btn-default pull-left" data-dismiss="modal">Close</button> 
     <button type="submit" class="btn btn-primary">Save changes</button> 
     </div> 
     </form> 

請注意,錯誤來自errorCallback功能

+0

響應字符串'ResponseEntity ( 「{\」 成功\ 「}」,'是不是有效的JSON修復它。 – georgeawg

+0

@georgeawg u能告訴我如何? –

+0

什麼是你的意圖?你這是什麼想要用戶接收? – georgeawg

回答

0

這是一個服務器端的問題,因爲反應是無效的角度
PS閱讀:閱讀@ georgeawg的最後一個註釋

-1

嘗試這種方式,HTML部分

$http({ 
     method: 'POST', 
     headers: { 
      'Content-Type': 'application/json' 
     }, 
     url: host + 'api/workflow/add', 
     data: angular.toJson($scope.wflow) 
    }).then(function (response) { 
     console.log(response); 
    }, function (response) { 
     console.log(response); 
    }); 
+1

總是出現同樣的錯誤 –

+0

你能否爲此提供蹲位所以我們可以解決問題 –

-1

可能導致此錯誤的原因響應是JSON字符串這樣的角度試圖解析JSON字符串轉換爲JSON。 我使用了transformResponse並且解決了錯誤。

$http({ 
    url : url, 
    method : 'POST', 
    data: data, 
    transformResponse: [ 
     function (data) { 
      return data; 
     } 
    ] 
})