javascript - How to deal with the gbk garbled code returned by nodejs when processing post requests?
巴扎黑
巴扎黑 2017-05-16 13:38:09
0
3
707

1. I built a local server using express and used webpack's proxyTable to forward the online interface.
2. The online interface background is java, and the returned data is in gbk format
3. The client initiates a post request and the data can be returned correctly (in the network)
4. Console.log or rendered in Chinese on the page It’s garbled code, how to solve it

I tried iconv-lite but it didn’t work. I don’t know if it’s written incorrectly

自己写的接口
apiRoutes.post('/hospitallist.xhtml',function(req,res){
  res.send(res)
})
会被转到xxx.com/hospitallist.xhtml
巴扎黑
巴扎黑

reply all(3)
我想大声告诉你

Finally, I solved it using the superagent method

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)
})
为情所困
res.charset = 'gbk';
res.send('some thing');
Ty80

To send data from the background to the front end, add

before instantiating the PrintWriter object
            response.setCharacterEncoding("GBK");
        然后再    PrintWriter writer=response.getWriter();
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template