报错: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported]

本文详细解析了在使用SpringMVC框架时,如何正确设置jQuery的$.ajax请求的contentType参数,避免HttpMediaTypeNotSupportedException异常,确保JSON数据能够被正确解析。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 控制台报如下消息提示:

Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported]

2019-09-11 22:48:32.829  WARN 12904 --- [nio-8080-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported]
2019-09-11 22:49:26.551  WARN 12904 --- [nio-8080-exec-5] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported]

说明控制器已经接收到了请求,jquery没有设置

contentType或直接设置为application/x-www-form-urlencoded;charset=UTF-8

因为传递的json格式的对象,应设置为 contentType: 'application/json;charset=UTF-8'

            $.ajax({
                url: "login",
                type: "POST",
                data: data,
                dataType: "json",
                async: false,
                contentType:'application/x-www-form-urlencoded;charset=UTF-8',//设置前
                contentType:'application/json;charset=UTF-8',  //设置后
                success: function (result) {
                    var newData = JSON.stringify(result);    //将json对象转换为字符串
                    newData = eval("(" + newData + ")");   //解析json
                    var msg = newData.msg;
                    alert(msg);
                },
                error: function () {
                    alert("提交请求失败!");
                }
            });

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值