ajax使用向Spring MVC发送JSON数据出现 org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/xwwwformurlencoded;charset=UTF8' not supported错误

ajax使用向Spring MVC发送JSON数据时,后端Controller在接受JSON数据时报org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported的错误。

解决办法是设置ajax的contentType为"application/json"

 1 $.ajax({
 2         $.ajax({
 3             url : urladdress,
 4             type : "POST",
 5             dataType : 'JSON',
 6             data :  JSON.stringify(JsonObj),
 7 
 8             //设置请求的contentType为"application/json"
 9             contentType: "application/json",
10 
11             success : function(response) {
12                 //处理返回的响应结果
13             }
14 });                                  

通过上述设置可以成功解决一些项目出现的这个问题,其他项目可能会有不一样或还有其他的解决办法。。。

原文地址:https://www.cnblogs.com/yourblog/p/8376807.html