javascript - nodejs處理post請(qǐng)求回的gbk亂碼怎么處理?
問題描述
1.自己用express搭建的本地服務(wù)器,利用webpack的proxyTable做了線上接口轉(zhuǎn)發(fā)。2.線上接口后臺(tái)是java,返回?cái)?shù)據(jù)是gbk格式3.客戶端發(fā)起post請(qǐng)求能正確返回?cái)?shù)據(jù)(network中)4.console.log或者渲染在頁面中中文都是亂碼,請(qǐng)問怎么解決
試了下iconv-lite不奏效,不知道是不是寫的不對(duì)
自己寫的接口apiRoutes.post(’/hospitallist.xhtml’,function(req,res){ res.send(res)})會(huì)被轉(zhuǎn)到xxx.com/hospitallist.xhtml
問題解答
回答1:最后還是用superagent的方法解決了
var charset = require(’superagent-charset’);var superagent = charset(require(’superagent’));function agent(req,res){ superagent.post(url+req.path) .type(’form’) .send(req.body) .set(’Accept’, ’application/json’) .charset(’gbk’) .end(function (err, sres) { var html = sres.text; res.send(html); });}app.post(’/list’,function(req,res,next){ agent(req,res)})回答2:
res.charset = ’gbk’;res.send(’some thing’);回答3:
后臺(tái)發(fā)送數(shù)據(jù)到前端,在實(shí)例化PrintWriter對(duì)象前加上
response.setCharacterEncoding('GBK');然后再 PrintWriter writer=response.getWriter();
相關(guān)文章:
1. mac連接阿里云docker集群,已經(jīng)卡了2天了,求問?2. docker安裝后出現(xiàn)Cannot connect to the Docker daemon.3. docker網(wǎng)絡(luò)端口映射,沒有方便點(diǎn)的操作方法么?4. docker-compose 為何找不到配置文件?5. vim - docker中新的ubuntu12.04鏡像,運(yùn)行vi提示,找不到命名.6. debian - docker依賴的aufs-tools源碼哪里可以找到啊?7. 為什么我ping不通我的docker容器呢???8. golang - 用IDE看docker源碼時(shí)的小問題9. angular.js - angularjs在兩個(gè)controller之間傳值,使用factory,為何不成功?10. docker images顯示的鏡像過多,狗眼被亮瞎了,怎么辦?

網(wǎng)公網(wǎng)安備