javascript - nodejs處理post請求回的gbk亂碼怎么處理?
問題描述
1.自己用express搭建的本地服務器,利用webpack的proxyTable做了線上接口轉發。2.線上接口后臺是java,返回數據是gbk格式3.客戶端發起post請求能正確返回數據(network中)4.console.log或者渲染在頁面中中文都是亂碼,請問怎么解決
試了下iconv-lite不奏效,不知道是不是寫的不對
自己寫的接口apiRoutes.post(’/hospitallist.xhtml’,function(req,res){ res.send(res)})會被轉到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:
后臺發送數據到前端,在實例化PrintWriter對象前加上
response.setCharacterEncoding('GBK');然后再 PrintWriter writer=response.getWriter();
相關文章:
1. css - 新手做響應式布局, 斷點過后右側出現空白,求幫助,謝謝。2. javascript - 關于<a>元素與<input>元素的JS事件運行問題3. css3 - 純css實現點擊特效4. mysql - 查詢字段做了索引為什么不起效,還有查詢一個月的時候數據都是全部出來的,如果分拆3次的話就沒問題,為什么呢。5. mysql - 記得以前在哪里看過一個估算時間的網站6. 大家好,我想請問一下怎么做搜索欄能夠搜索到自己網站的內容。7. ID主鍵不是自增的嗎 為什么還要加null8. MySQL中的enum類型有什么優點?9. python - 啟動Eric6時報錯:’qscintilla_zh_CN’ could not be loaded10. javascript - vue 怎么渲染自定義組件
