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. docker鏡像push報(bào)錯(cuò)2. angular.js - angular內(nèi)容過長(zhǎng)展開收起效果3. angular.js - angularjs的自定義過濾器如何給文字加顏色?4. python 怎樣用pickle保存類的實(shí)例?5. python的前景到底有大?如果不考慮數(shù)據(jù)挖掘,機(jī)器學(xué)習(xí)這塊?6. MySQL中無法修改字段名的疑問7. javascript - 微信小程序限制加載個(gè)數(shù)8. 大家好,請(qǐng)問在python腳本中怎么用virtualenv激活指定的環(huán)境?9. linux - 升級(jí)到Python3.6后GDB無法正常運(yùn)行?10. 并發(fā)模型 - python將進(jìn)程池放在裝飾器里為什么不生效也沒報(bào)錯(cuò)
