zoukankan      html  css  js  c++  java
  • ssm框架ajax发送数据报错Content type 'application/json' not supported

    ajax代码:

    var data = {
          "name":"aaa",
          "password":123
    };
    
    $.ajax({
          url:'/ems/test',
          data:JSON.stringify(data),
          type:'post',
          dataType:"json",
          contentType:'application/json;charset=utf-8',
          success:function(data){
                //其他逻辑
          }
    })
    

    controller代码:

    @ResponseBody
    @RequestMapping(value = "test",produces = "application/json";charset=utf-8")
    public String test(@RequestBody Staff staff){
          //增删改查
    }
    

    首先解决这个错误需要在ajax代码中添加

    dataType:"json",
    contentType:'application/json;charset=utf-8',
    

    然后再pom.xml中添加依赖

            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.9.9</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-core</artifactId>
                <version>2.9.9</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-annotations</artifactId>
                <version>2.9.9</version>
            </dependency>
    

    之后在springmvc.xml中加入配置

    <!-- 设置json转换器 -->
        <bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        </bean>
        <!-- 设置string转换器 -->
        <bean id="stringConverter"
              class="org.springframework.http.converter.StringHttpMessageConverter">
            <property name="writeAcceptCharset" value="false" />
    
            <property name="supportedMediaTypes">
                <list>
                    <value>text/html;charset=UTF-8</value>
                </list>
            </property>
        </bean>
        <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
            <property name="messageConverters">
                <list>
                    <ref bean="stringConverter" />
                    <ref bean="jsonConverter" />
                </list>
            </property>
        </bean>
    
  • 相关阅读:
    tomcat内存溢出问题解决
    redis知识点汇总
    activiti全部知识点
    Python_Note_Preview_03_URL
    S&P_09_协方差(协方差矩阵)与相关系数
    Linear_algebra_06_ 内积空间
    Linear_algebra_05_线性方程组的解理论
    Linear_algebra_04_向量空间
    Linear_algebra_03_矩阵
    Linear_algebra_02_行列式
  • 原文地址:https://www.cnblogs.com/sfencs-hcy/p/13467777.html
Copyright © 2011-2022 走看看