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或者渲染在頁(yè)面中中文都是亂碼,請(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. javascript - js 寫一個(gè)正則 提取文本中的數(shù)據(jù)2. javascript - 為什么我的vue里的router-link不起作用3. javascript - js一個(gè)小的算法問題,找個(gè)好一點(diǎn)的答案。4. javascript - vue子路由匹配渲染到頂級(jí)視圖層問題5. 算法 - python 給定一個(gè)正整數(shù)a和一個(gè)包含任意個(gè)正整數(shù)的 列表 b,求所有<=a 的加法組合6. javascript - 游戲里物體角色層次渲染邏輯和代碼怎么寫才好?7. javascript - 發(fā)現(xiàn)個(gè)奇怪的問題,寫的css動(dòng)畫當(dāng)我把標(biāo)簽頁(yè)收起時(shí)動(dòng)畫自動(dòng)暫停了8. javascript - 新組成的數(shù)組打印出來出現(xiàn)問題,里面有對(duì)象,但長(zhǎng)度為空9. javascript - 關(guān)于Lazyload遇到的問題10. javascript - 如果根據(jù)參數(shù)給table中的tr綁定不同事件
