我有时会被一些琐碎的东西困住,但我无法正常工作。我有一个AngularJS控制器,该控制器通过rest调用backen Spring引导应用程序。我可以确认,如果我通过Postman向后端提出请求,那么它运行良好,只是当通过html页面完成时,它根本无法正常工作,我得到了415。
var survey = angular.module('survey', []);
var surveyDto = {
age:"",
sex:"",
status:"",
chronic:"",
keyWorker:""
};
survey.controller('surveyController', function($scope, $http) {
$scope.sendAnswers = function(surveyDto) {
$http(
{
method: 'POST',
url: 'http://localhost:8080/surveys/new',
data: angular.toJson(surveyDto),
headers: {'Content-Type': 'application/json'}
}
);
};
});
在后端,我有一个非常简单的Java控制器:
@RequestMapping(value = "/surveys/new", consumes = "application/json", produces = "application/json", method = RequestMethod.POST)
public ResponseEntity<NewSurveyResponse> newSurvey(HttpServletRequest request) {
//...
}
当网页使用控制器发送请求时,我得到了415(不受支持的媒体类型)。
我不明白为什么。在尝试在控制器中使用自己的dto对象表示数据之前,但还是没有用。我不确定缺少什么。我在某处阅读了可以使用HttpServletRequest的内容,但也无法正常工作。有什么主意吗?
我正在使用Spring和AngularJS的以下版本-GoogleApis Angular 1.7.9-春季靴2.2.4
错误消息说您的Content-Type为”,为空,这使我相信您在Angular调用中将其指定为错误。
尝试一下-或其变体。
let headers = new HttpHeaders({
'Content-Type': 'application/json'});
let options = { headers: headers };
$http.post("http://localhost:8080/surveys/new", surveyDto, options);