search

Home  >  Q&A  >  body text

javascript - How to deal with the gbk garbled code returned by nodejs when processing post requests?

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
巴扎黑巴扎黑2782 days ago719

reply all(3)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-05-16 13:40:09

    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)
    })

    reply
    0
  • 为情所困

    为情所困2017-05-16 13:40:09

    res.charset = 'gbk';
    res.send('some thing');

    reply
    0
  • PHP中文网

    PHP中文网2017-05-16 13:40:09

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

    before instantiating the PrintWriter object
                response.setCharacterEncoding("GBK");
            然后再    PrintWriter writer=response.getWriter();

    reply
    0
  • Cancelreply