微信小程序中,POST,FormData格式传递参数

现状

后台接口使用的POST提交方式,FormData传参,而微信小程序中没有FormData这个函数,后台如果改动JSON格式的话,改动较大,所以没办法改动成JSON,那只能从接口方面入手了。
网上搜了很多FormdData格式传参的,要导入这个js文件的,要拼接字符串的都有,无奈后台接收到的参数却是为空,所以我模拟通过web调用接口,根据请求头来设置微信请求中的请求头设置。

代码

后台代码

@PostMapping(value = "/methodurl")
@ResponseBody
methodName(Integer page, Integer size,String state,HttpServletRequest request) {
    
    }

web请求

Request Headers:
	Accept: application/json, text/javascript, */*; q=0.01
	Accept-Encoding: gzip, deflate, br
	Accept-Language: zh-CN,zh;q=0.9
	Connection: keep-alive
	Content-Length: 22
	Content-Type: application/x-www-form-urlencoded; charset=UTF-8
	Cookie: ****
	Host: localhost:8080
	Origin: http://localhost:8080
	Referer: http://localhost:8080/methodurl
	sec-ch-ua: "Not?A_Brand";v="8", "Chromium";v="108", "Google Chrome";v="108"
	sec-ch-ua-mobile: ?0
	sec-ch-ua-platform: "Windows"
	Sec-Fetch-Dest: empty
	Sec-Fetch-Mode: cors
	Sec-Fetch-Site: same-origin
	User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36
	X-Requested-With: XMLHttpReques

Payload:
	page=1&size=10&state=0

微信小程序(正文,可直接跳到此处)

就这样写直接接可以了,其他什么都不用设置,

data: 'page=1&size=10&state=0',
header: {
  'content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
},
method: "POST",

猜你喜欢

转载自blog.csdn.net/qq_16607641/article/details/128715565
今日推荐