springmvc后台接收ajax参数

1.@RequestBody接收数据

前端:

$.ajax({
    url: "http://localhost:8081/aj",
    contentType: "application/json;charset=UTF-8",//后台用@RequestBody这句话一定要写
    type: 'post',
    dataType: 'json',
    data:JSON.stringify({phone:"13311111111",code:"906036"}),
    success: function(res){
        console.log(res)
    },
    error: function(){
    }
})

后台:

@RequestMapping("/aj")
@ResponseBody
public Object ajaxTest(@RequestBody JSONObject param){//这个地方JSONObject 可以换成Map param
    System.out.println(param);
    return "ok";
}

输出:

springmvc后台接收ajax参数_第1张图片

 


2.实体类接收数据

前端:

$.ajax({
    url: "http://localhost:8081/aj2",
    // contentType: "application/json;charset=UTF-8",
    type: 'post',
    dataType: 'json',
    data:
         {phone:"13311111111",code:"906036"}
    ,
    success: function(res){
        console.log(res)
    },
    error: function(){
    }
})

后台:

@RequestMapping("/aj2")
@ResponseBody
public Object ajaxTest2(A param){//实体类接收
    System.out.println(param);
    return "ok";
}

输出:

springmvc后台接收ajax参数_第2张图片

实体类:

public class A {
    private String phone;
    private String code;

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }
}

 


3.单独接收单个数据

这个可以直接在地址后面拼 http://localhost:8081/aj3?phone=123&code=123

前端:

$.ajax({
    url: "http://localhost:8081/aj3",
    // contentType: "application/json;charset=UTF-8",
    type: 'post',
    dataType: 'json',
    data:
       {phone:"13311111111",code:"906036"}
    ,
    success: function(res){
        console.log(res)
    },
    error: function(){
    }
})

后台:

@RequestMapping("/aj3")
@ResponseBody
public Object ajaxTest3(String phone , String code){
    System.out.println("phone:"+phone + "   code:"+code);
    return "ok";
}

输出:

springmvc后台接收ajax参数_第3张图片

 


4.form表单实体类接收数据

前端:

phone:
code:
$.ajax({
    url:"http://localhost:8081/aj4",
    // contentType: "application/json;charset=UTF-8",
    data:$("#form1").serialize(),
    dataType:"json",
    async: true,
    type:"post",
    success:function(res){
    }
})

后台:

@RequestMapping("/aj4")
@ResponseBody
public Object ajaxTest4(A params){
    System.out.println(params.toString());
    return "ok";
}

输出:

springmvc后台接收ajax参数_第4张图片

 

 


5.restful风格
前端:
$.ajax({
    url: "http://localhost:8081/aj5/133111111/909036",
    // contentType: "application/json;charset=UTF-8",
    type: 'post',
    dataType: 'json',
    data: {
    }
    ,
    success: function(res){
        console.log(res)
    },
    error: function(){
    }
})

后台:

@RequestMapping("/aj5/{phone}/{code}")
@ResponseBody
public Object ajaxTest5(@PathVariable("phone") String phone,@PathVariable("code") String code){
    System.out.println("phone:"+phone + "   code:"+code);
    return "ok";
}

输出:

springmvc后台接收ajax参数_第5张图片

 

6.list接收

@RequestMapping("/postList")
    @ResponseBody
    public String postList(@RequestBody List testL){
        System.out.println(testL);
        return null;
    
    }

 

var testList=[];
var user={};
user.id=1;
user.name='jack';
testList.push(user);
var user2={};
user2.id=2;
user2.name='tom';
testList.push(user2);
$.ajax({
    // headers必须添加,否则会报415错误
    headers: {
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    },
  type: 'POST',
  dataType: "json", //表示返回值类型,不必须
  data: JSON.stringify(testList),
  url: '/test/postList',
  success: function(){
      alert('success');
  }
  
});

需要注意点:1、参数是数组类型

      2、传入data时,转换 JSON.stringify(testList)

      3、必须有headers: {

                'Accept': 'application/json',
                'Content-Type': 'application/json'
            }

 

 

 

 


总结:这几种方法比较常用,偏基础接收数据,

contentType: "application/json;charset=UTF-8"这句话小心使用

你可能感兴趣的:(开发笔记)