鍍金池/ 問答/網絡安全  HTML/ superagent post提交表單數據到數據庫中文亂碼

superagent post提交表單數據到數據庫中文亂碼

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

亂碼如下圖所示:

圖片描述

回答
編輯回答
淚染裳

不要被埋啊啊啊~

2017年8月1日 10:31
編輯回答
硬扛

網頁客戶端 和 服務器端php 需要 相同編碼,推薦 utf-8 ,如果都是 gb2312 ,你 PHP 也需要設置為 gb2312~

2017年3月22日 21:41