欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringMVC ajax请求出现415异常

程序员文章站 2024-01-25 19:48:46
...

在写ajax请求时出现了415异常


```javascript
 $("#btn4").click(function () {
                //准备好要发送到服务器端的数据
                var student = {
                    stuId: 5,
                    stuName: "tom",
                    address: {
                        province:"广东",
                        city:"深圳",
                        street:"后瑞"
                    },
                    subjects: [
                        {
                            subjectName: "JavaSE",
                            subjectScore: 100
                        }, {
                            subjectName: "SSM",
                            subjectScore: 99
                        }
                    ],
                    "map": {
                        k1: "v1",
                        k2: "v2"
                    }
                };
                //将JSON数组转换为JSON字符串
                var requestBody = JSON.stringify(student);
                alert(typeof(requestBody))

                $.ajax({
                    url: "send/compose/object.json",
                    type: "post",
                    contentType: "application/json;charset=utf-8",
                    dataType: "json",
                    data: requestBody,
                    success: function (response) {
                        console.log(response);
                        alert("成功");
                    },
                    error: function (response) {
                        console.log(response);
                        alert("失败");
                    }
                });
            });
常见的原因是:contentType: "application/json;charset=UTF-8",
但是我加了以后还是没有解决,
查找了半天的原因,发现是SpringMVC配置文件中配置SpringMVC注解驱动时引错了依赖
<mvc:annotation-driven/>
正确的是
xmlns:mvc="http://www.springframework.org/schema/mvc"
所以说还是SpringBoot方便啊hhh,害我找了半天的错

ajax出问题,一个是注解驱动一个是jackson包这两个地方容易忽略
相关标签: java ajax