gpt4 book ai didi

angularjs - 如何解决 http baddata Angular 错误?

转载 作者:行者123 更新时间:2023-12-02 20:44:32 24 4
gpt4 key购买 nike

我正在开发一个在前端使用 Angular 并在后端使用 J2EE 的应用程序,我制作了一个表单,我必须在其中发布要保存在数据库中的数据
问题是帖子工作正常,但添加后我无法获得服务器响应,我总是收到此错误(奇怪的是,错误通常是灰色而不是红色)

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)

这是 Angular 代码

$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);
}

如果需要的话,这是 html 部分

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

最佳答案

可能会导致此错误,因为响应位于 json 字符串中,因此尝试将该 json 字符串解析为 JSON。我使用了transformResponse并解决了错误。

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

关于angularjs - 如何解决 http baddata Angular 错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44945234/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com