javascript - nodejs處理post請(qǐng)求回的gbk亂碼怎么處理?
問(wèn)題描述
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或者渲染在頁(yè)面中中文都是亂碼,請(qǐng)問(wèn)怎么解決
試了下iconv-lite不奏效,不知道是不是寫的不對(duì)
自己寫的接口apiRoutes.post(’/hospitallist.xhtml’,function(req,res){ res.send(res)})會(huì)被轉(zhuǎn)到xxx.com/hospitallist.xhtml
問(wèn)題解答
回答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. 如何修改phpstudy的phpmyadmin放到其他地方2. python - Django 表單問(wèn)題?3. javascript - 百度搜索網(wǎng)站,如何讓搜索結(jié)果顯示一張圖片加上一段描述,如圖;求教4. css3 - 這個(gè)右下角折角用css怎么畫出來(lái)?5. 索引 - 請(qǐng)教下Mysql大數(shù)據(jù)量的聯(lián)合查詢6. pip安裝提示Twisted錯(cuò)誤問(wèn)題(Python3.6.4安裝Twisted錯(cuò)誤)7. 關(guān)于Mysql聯(lián)合查詢8. javascript - main head .intro-text{width:40%} main head{display:flex}為何無(wú)效?9. mysql - sql 語(yǔ)句更改表結(jié)構(gòu),添加多個(gè)列,怎么寫?10. javascript - 網(wǎng)頁(yè)中嵌套iframe,網(wǎng)頁(yè)和iframe viewport不同,怎么能讓iframe中的網(wǎng)頁(yè)不變形
