我正在尝试将一个字符串从我的 View 发送到我的 Controller ,但我一直收到“文本”不存在的错误。
这是我的javascript
$scope.sendMsg = function(){
console.log($scope.my.message);
data = {"text" : $scope.my.message};
$http({
method:'POST',
data:data,
contentType: "application/json; charset=utf-8",
dataType:"json",
url:'/post-stuff'
}).then(function(response){
console.log(response);
});
}
我的休息 Controller :
@RequestMapping(value= "/post-stuff", method=RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public ResponseEntity<PostTextResult> postStuff(@RequestParam(value= "text") String text){
System.out.println(text);
return new ResponseEntity<PostTextResult>(stuff.postTextContent(text), HttpStatus.OK);
}
最佳答案
您可以尝试将当前 data
中的前端更改为:
data = {params:{"text" : $scope.my.message}};
Spring 需要知道请求有参数。
或者您可以尝试将 Controller 中的后端 @RequestParam(value= "text") String text
更改为 @RequestBody String text
查看 RequestBody and RequestParam 之间的区别
关于javascript - 必需的字符串参数 'text' 不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44729384/