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. html - 在一個table表單中 td用v-for 使用v-if判斷是否顯示 然后用一個外部的button 判斷點擊最后一行隱藏2. docker-compose中volumes的問題3. css - chrome下a標簽嵌套img 顯示會多個小箭頭?4. docker網絡端口映射,沒有方便點的操作方法么?5. mysql - 在不允許改動數據表的情況下,如何優化以varchar格式存儲的時間的比較?6. vim - docker中新的ubuntu12.04鏡像,運行vi提示,找不到命名.7. java - 關于File的問題?8. javascript - 網頁打印頁另存為pdf的代碼一個問題9. 提示內部服務錯誤什么問題10. 建議首頁視頻往頂部放
