我在整个互联网上搜索了一个解决方案,没有任何效果。 当我尝试使用ajax发布一个course-object时,我一直收到406 - Not Acceptable错误。
错误说明:
此请求标识的资源只能生成 根据请求具有不可接受的特征的回复 "接受"头。
奇怪的是,我第二次发送请求时得到500 - > "课程"已经存在。因此,对象将保存在服务器端。
ajax请求:
function addCourse() {
var name = $('#course_name').val();
var sp = $('#course_sp').val();
var h_sp = $('#course_hour_sp').val();
var student_id = $('#studentID').val();
var information = {studentId: student_id, name: name, studyPoints: sp,
hoursPerStudyPoint: h_sp, studiedHours:"00:00:00"};
$.ajax({
url: "studentView/addCourse.htm",
data: JSON.stringify(information),
dataType: 'json',
contentType : 'application/json; charset=utf-8',
type: 'POST',
success: function (data) {
$('#course_name').val("");
$('#course_sp').val("");
$('#course_hour_sp').val("");
addNewCourse(data);
},
error: function (e) {
console.log(e);
}
});
}
控制器:
@Controller
@RequestMapping("/studentView")
public class CoursesController {
private final Service service;
@Autowired
public CoursesController(Service service){
this.service = service;
}
@RequestMapping(value="/addCourse", method = RequestMethod.POST)
public @ResponseBody Course createCourse(@RequestBody Course course) throws
DatabaseException{
service.addCourse(course);
return course;
}
}
POM依赖关系:
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>productive-studying-service</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.0.pr4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0.pr4</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>${javaee.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
提前谢谢。
答案 0 :(得分:0)
您正在将url: "studentView/addCourse.htm",
用于ajax代码,您需要使用'url:“studentView / addCourse”,'代码,从网址中删除.htm
。