V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
yuuk520
V2EX  ›  问与答

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

  •  
  •   yuuk520 · 2018-05-15 17:13:33 +08:00 · 1166 次点击
    这是一个创建于 2164 天前的主题,其中的信息可能已经有所发展或是发生改变。

    求助大佬! 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。

    乱码如下图所示:

    图片描述

    1 条回复    2018-05-15 22:07:13 +08:00
    jam1024
        1
    jam1024  
       2018-05-15 22:07:13 +08:00
    为何不统一用 UTF-8
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5347 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 07:28 · PVG 15:28 · LAX 00:28 · JFK 03:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.