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中添加依赖

        
            com.fasterxml.jackson.core
            jackson-databind
            2.9.9
        
        
            com.fasterxml.jackson.core
            jackson-core
            2.9.9
        
        
            com.fasterxml.jackson.core
            jackson-annotations
            2.9.9
        

之后在springmvc.xml中加入配置


    
    
    
    
        

        
            
                text/html;charset=UTF-8
            
        
    
    
        
            
                
                
            
        
    

你可能感兴趣的:(ssm框架ajax发送数据报错Content type 'application/json' not supported)