前后端交互

1.参数传递

后端,这种事传几个参数进来的,不是json

@PostMapping("/list")
public Result<?> findPage(@RequestParam(defaultValue = "1") Integer current,
                             @RequestParam(defaultValue = "10") Integer size,
                             @RequestParam(defaultValue = "") String search) {
       System.out.println("pageNum:"+current);
       System.out.println("pageSize:"+size);
       System.out.println("search:"+search);
}

apipost工具,后端能拿到三个值 content-type:application/x-www-form-urlencoded

前后端交互_第1张图片

前端

this.$request({
       url: "/order/list",
       method: "POST",
       params: {//这样传的是content-type:application/x-www-form-urlencoded
         "current":1,
         "size":5,
         "search":"张"
       }
}).then(res => {
       console.log(res.data)
})

前后端交互_第2张图片

2.参数传递2

@RequestMapping("/login")
    public Result<?> login(
            @RequestParam(value = "code", required = false) String code,
            @RequestParam(value = "rawData", required = false) String rawData,
            @RequestParam(value = "signature", required = false) String signature,
            @RequestParam(value = "encryptedData", required = false) String encryptedData,
            @RequestParam(value = "iv", required = false) String iv
    ) {
        System.out.println("code:" + code);
        System.out.println("rawData:" + rawData);
        System.out.println("signature" + signature);
        System.out.println("encryptedData" + encryptedData);
        System.out.println("iv" + iv);
uni.request({
									url: 'http://localhost:8080/renren-fast/apartment/user/login',
									method: "POST",
									header:{
										"content-type":"application/x-www-form-urlencoded"
									},
									data: {
										code: infoRes.code, //临时登录凭证
										rawData: rawData, //用户非敏感信息
										signature: signature, //签名
										encryptedData: encryptedData, //用户敏感信息
										iv: iv //解密算法的向量
									},
									success: (res) => {
										console.log(res)

									}
								})
							}

3.json传递

后端,传json,对象形式

@PostMapping("/save")
public Result<User> save(@RequestBody User user) {

}

apipost工具,json形式传对象, content-type:application/json

前后端交互_第3张图片

前端

this.$request({
        url: "/order/list",
        method: "POST",
        data: {//这样传的是content-type:application/json
          "current":1,
          "size":5,
          "search":"张"
        },
}).then(res => {
  		console.log(res.data)
})

前后端交互_第4张图片

4.ajax json传递

$.ajax({
            type: 'post',
            url: '${pageContext.request.contextPath}/parkingTimeController/addTime',
            contentType: "application/json;charset=utf-8",
            data:JSON.stringify({
                "carId": carId,
                "time":time,
                "pId": pId
            }),
            //     {
            //     "carId":carId,
            //     "beginTime":time,
            //     "pId":pId
            // },
            dataType: 'json',
            success: function (result) {


            }

        })

你可能感兴趣的:(mybatis,linux,bash)