我是Angular JS的新手。我试图使用角度js从jsp页面提交数据到spring mvc控制器。我正在尝试将数据注入Pojo对象。但我得到例外说'415不支持'。我帮我错了。
JSP页面:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Testing Tool</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>
<script type="text/javascript">
var apps=angular.module('sampleApps',[]);
apps.controller('formController',function($scope, $http)
{
$scope.register=function()
{
var name=$scope.name;
var address=$scope.address;
alert(name + " , "+address);
var formData = {
name : name,
address : address
};
$http({
method : "POST",
url : "userdetails",
contentType: 'application/json',
mimeType: 'application/json',
/* data: {name: name,address:address} */
data:formData
}).then(function mySucces(response) {
//$scope.myWelcome = response.data;
alert("Success");
}, function myError(response) {
alert(response);
// $scope.myWelcome = response.statusText;
});
};
});
</script>
</head>
<body data-ng-app="sampleApps">
<div ng-controller="formController">
Name: <br/>
<input type="text" name="name" ng-model="name"/><br/>
Address: <br/>
<input type="text" name="address" ng-model="address"/><br/>
<input type="button" value="submit" ng-click="register()">
</div>
</body>
</html>
控制器:
@RequestMapping(value="/userdetails")
public @ResponseBody User getUserDetails(HttpServletRequest request,@RequestBody User data)
{
System.out.println(data);
return data;
}
Pojo课程:
public class User implements Serializable
{
/**
*
*/
private static final long serialVersionUID = 1L;
private String name;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
答案 0 :(得分:0)
我认为您需要添加数据:formData =&gt;的数据:JSON.stringify(FORMDATA)强>
检查这是否有帮助
干杯!
答案 1 :(得分:0)
基于你的标签,我认为你使用的是Spring MVC。只需为spring web包添加一个新的loggger。将包org.springframework.web
的日志级别提升为DEBUG将为您提供415错误的根本原因。
这是一个使用logback的例子:
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<logger name="org.springframework.web" level="DEBUG" />
<root level="info">
<appender-ref ref="STDOUT" />
</root>
</configuration>
您还可以查看HTTP请求解决的所有请求映射。
祝你好运。