ajax与Post请求 参数的前后台传值/接收

闲着来测试一轮~

1. 后台@PostMapping,参数无注解

java代码:

	/**
     * 测试方法1
     * @param a
     * @param b
     */
    @PostMapping("/testMethod1")
    public void testMethod1(String a, String b){
        if (a.isEmpty() || b.isEmpty()){
            throw new BusinessException("500","参数不合法");
        }
    }

(1).参数在路径,post方式几乎不会用到

PostMan测试:
在这里插入图片描述
debug断点,参数值:
在这里插入图片描述

(2). content-type为application/x-www-form-urlencoded

PostMan测试:
在这里插入图片描述
debug断点,参数值:
在这里插入图片描述
ajax测试:
ajax的contentType参数默认值为:“application/x-www-form-urlencoded”

$.ajax({
       url:"/testMethod1",
       contentType:"application/x-www-form-urlencoded",
       data:{"a":"111","b":"123"},
 	   type:"POST",
       success:function (data) {
           console.log("success");
       },error:function () {
		   alert("!");
       }
     })

运行结果:
在这里插入图片描述

2. 后台@PostMapping+@RequestParam注解接收值

前台js:

let sendData = {"workbenchId":$(th).attr('data-val'),"locationId":$("#locationId").val()};

$.ajax({
         url:"/testMethod2",
           contentType:"application/x-www-form-urlencoded",
           data:{"a":"111","b":"123"},
  		   type:"POST",
           success:function (data) {
               console.log("success");
           },error:function () {
			   alert("!");
           }
         })

后台部分:

	/**
     * 测试方法2
     * @param a
     * @param b
     */
    @PostMapping("/testMethod2")
    public void testMethod2(@RequestParam String a, @RequestParam String b){
        if (a.isEmpty() || b.isEmpty()){
            throw new BusinessException("500","参数不合法");
        }
    }

测试结果:
在这里插入图片描述

3. 后台@PostMapping+@ResponseBody+@RequestBody注解

后台部分:

	/**
     * 测试方法1
     * @param map
     */
    @PostMapping("/testMethod3")
    @ResponseBody
    public void testMethod3(@RequestBody Map map){
        if (map == null || !map.containsKey("a") || !map.containsKey("b")){
            throw new BusinessException("500","参数不合法");
        }
    }

ajax部分:


let json = {"a":"GZ201904020060","b":"402883946bb5997e016bb5a66a14000c"};

$.ajax({
   url:"/testMethod3",
   contentType:"application/json;",
   data:JSON.stringify(json),
   type:"POST",
   success:function (data) {
       console.log("success");
   },error:function () {
 	   alert("!");
   }
})

debug断点,参数获取:
在这里插入图片描述
PostMan测试:
在这里插入图片描述

  • 6
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值