superagent post 提交表单数据到数据库中文乱码

2018-05-15 17:13:33 +08:00
 yuuk520

求助大佬! superagent post 提交表单数据到数据库中文乱码,代码如下:

const headers = {
	"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
	"Accept-Encoding": "gzip, deflate",
	"Accept-Language": "zh-CN,zh;q=0.9,en;q=0.8",
	"Connection": "keep-alive",
	"Cache-Control": "no-cache",
	'User-Agent': Util.randomUserAgent()
};
const basicFormData = {
    'id': 0,
    'enews': 'AddNews',
    'classid': 14,
    'bclassid': 10,
    'oldchecked': 0,
    'ecmsnfrom': 1,
    'ecmscheck': 0,
    'havetmpic': 0,
    'isgood': 0,
    'firsttitle': 0,
    'dokey': 1,
    'copyimg': 1,
    'getfirsttitlepic': 1,
    'getfirsttitlespicw': 375,
    'getfirsttitlespich': 250
};

const publishFormData = {
    'title': '测试标题,测试标题测试标题测试标题',
    'keyboard': 'aaa,bbb,cccc',
    'smalltext': '测试描述 111 的送达方式方法是',
    'newstext': '内容内容内容'
};
agent
.post('submit.php')
.set(headers)
.set('Content-Type', 'multipart/form-data;')
.type('form')
.send(Object.assign(basicFormData, publishFormData))
.charset('gb2312')
.then(result => {
    console.log(result.text);
})
.catch(err => {
    console.error(err);
})

网站编码是 gb2312,设置了 charset 为 gb2312,返回 result.text 的内容编码是正常的,但是模拟提交表单到数据库后就乱码了,数据库用是 mysql,数据库编码是 utf8。

乱码如下图所示:

1170 次点击
所在节点    问与答
1 条回复
jam1024
2018-05-15 22:07:13 +08:00
为何不统一用 UTF-8

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/455022

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX