前后端分离 - 前端传JSON后端接收到%7B%22...的问题解决

一、问题重现

在这里插入图片描述

1. 前端代码:

		//测试提交json
		$('.test').click(function () {
			let liaison = $('#liaison').val();
			let mobile = $('#mobile').val();
			let map = {
				"liaison": liaison,
				"mobile": mobile
			};
			let json = JSON.stringify(map);
			console.log(json);

			$.ajax({
				url: '/..../testJson',
				data: json,
				type: 'post',
				async: false,
				success: function (result) {
					console.log(result);
				}
			})

2. 后端代码

/**
	 * 测试json数据
	 */
	@PostMapping("/testJson")
	@ResponseBody
	public ApiResult testJson(@RequestBody String json){
		log.info("json =========== {}", json);
		return ApiResult.success();
	}

3. 发送数据后得到结果截图

3-1. 前端console.log

在这里插入图片描述

3-2. 后端 log.info

在这里插入图片描述

二、出现原因

其实一看便知,是编码格式的问题。那么分两步走。

三、问题解决

1. 首先设置一下软件的编码格式,idea和webstorm一样设置

位置: file -> settings

2. 前端页面数据编码格式设置

很简单,在ajax里面添加一句 contentType 搞定,添加后的ajax是这样的

			$.ajax({
				url: '/..../testJson',
				data: json,
				type: 'post',
				async: false,
				contentType: 'application/json;charset=utf-8',
				success: function (result) {
					console.log(result);
				}
			})

四、结果截图

在这里插入图片描述

END

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值