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. mysql優化 - mysql count(id)查詢速度如何優化?2. angular.js - 不適用其他構建工具,怎么搭建angular1項目3. mysql主從 - 請教下mysql 主動-被動模式的雙主配置 和 主從配置在應用上有什么區別?4. python - django 里自定義的 login 方法,如何使用 login_required()5. 主從備份 - 跪求mysql 高可用主從方案6. java8中,邏輯與 & 符號用在接口類上代表什么意思7. python如何不改動文件的情況下修改文件的 修改日期8. angular.js - angular-ui-bootstrap 報錯無法使用?9. node.js - node_moduls太多了10. python - 關于ACK標志位的TCP端口掃描的疑惑?
