How to get UTF-8 in Node.js?

后端 未结 3 1561
感情败类
感情败类 2020-12-14 02:01

How do I get UTF-8 support on my API? At the moment, a string outputs like this:

name: \"John D�m\"

Instead of:

name: \"Joh         


        
相关标签:
3条回答
  • 2020-12-14 02:12

    Hook into you response generator or create a middleware that does the following:

    res.header("Content-Type", "application/json; charset=utf-8");
    

    Otherwise the browser displays the content in his favorite encoding.

    If this doesn't help you DB is probably in the wrong encoding.

    Edit: Since the answer is nearly 5 years old, the API has changed. For current node.js versions use:

    res.setHeader("Content-Type", "application/json; charset=utf-8");
    
    0 讨论(0)
  • 2020-12-14 02:16

    I can't solve this problem with setting content type. I solved this problem with encode function.

    res.cookie('someCookie', someCookie, {
      encode: c => c,
    });
    

    For more information: express cookie

    ExpressJS version: 4.16.4

    0 讨论(0)
  • 2020-12-14 02:19

    My problem solved with this:

    res.writeHeader(200 , {"Content-Type" : "text/html; charset=utf-8"});

    0 讨论(0)
提交回复
热议问题