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. php+mysql 高并发 根据id 循环下载数据如何避免重复 (接口)2. mysql多表查询3. PHP搜索功能的实现 哪里错了4. 请问一下,图片上传成功,但是后台对应文件夹里面却没有图片,这是什么原因?(已部署到服务器)5. 下载32位vc9和11、14运行库时解压错误6. php - SQL 一条语句查询出文章和对应的文章标签7. mysql优化 - mysql like语句会导致全表扫描?8. java - mysql缓存问题9. 电脑上装了wamp后,再装了mysql,wamp上的mysql就不能用了10. angular.js使用$resource服务把数据存入mongodb的问题。

网公网安备