javascript - nodejs处理post请求回的gbk乱码怎么处理?

查看:118
本文介绍了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

解决方案

最后还是用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)
})

这篇关于javascript - nodejs处理post请求回的gbk乱码怎么处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆