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. node.js - nodejs+express+vue2. javascript - vue2.0中使用vue2-dropzone的demo,vue2-dropzone的github网址是什么??百度不到。3. objective-c - ios 怎么实现微信联系列表 最好是swift4. macos - mac下docker如何设置代理5. javascript - h5 video层级太高导致浮在div上面,如何解决?6. Android TextView 或 ListView 加载过渡效果7. 自己安装了apache2.2,但是重启apache后出错了,求解!谢谢!8. vue计算属性怎么样与for结合使用9. angular.js - vue/react 渲染内容抖动10. PHP能实现百度网盘的自动化么?
