What is 415 ?
HTTP 415 Unsupported Media Type
The client error response code indicates that the server refuses to accept the request because the payload format is in an unsupported format.
Let’s look at the broswer ?
How to resolve 405 problem when using ajax post @ResponseBody return json data ?
if you use Spring 4.x jar,please following me(Maven Repository-Spring 4.x.jar)
add related jar package
spring-webmvc.x.x.jar
jackson-databind.jar
jackson-core.jar
jackson-annotations.jarIn Spring Configuration file,add following code
<bean class="org.springframework.web.servlet.mvc.
annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonHttpMessageConverter" />
</list>
</property>
</bean>
<bean id="jsonHttpMessageConverter" class="org.springframework.http.converter.json.
MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
- In ajax , set contentType : ‘application/json;charse=UTF-8’
var data = {"name":"jsutin","age":18};
$.ajax({
url : "/ASW/login.html",
type : "POST",
data : JSON.stringify(data),
dataType: 'json',
contentType:'application/json;charset=UTF-8',
success : function(result) {
console.log(result);
}
});
- In server ,using @RequestBody anotation receive json data,and using @ResponseBody anotation response json to jsp page
@RequestMapping(value = "/login",method = RequestMethod.POST)
public @ResponseBody User login(@RequestBody User user){
system.out.println(user.getName);
system.out.println(user.getAge);
}
本文介绍了如何解决HTTP415错误,该错误通常出现在服务器拒绝请求,因为请求的负载格式不受支持。文章提供了使用Spring框架的具体解决方案,包括配置文件的设置及前端Ajax请求的正确方式。
3450

被折叠的 条评论
为什么被折叠?



