Pizhev Racing
Pizhev Racing

Reputation: 486

How to see cyrillic data in json use node.js restful API

I have a cyrillic data in json format and in the API I see the data like this:

stations    
0   
Map_code    0
NAME_IME    "Bjala river - Smoljan"
NAME_CYR    "���� ���� - ��. ������"
Alertlevelwebsite   1
CODENUM 1

I want to display NAME_CYR "���� ���� - ��. ������" with UTF-8 or CP_1251 encode but I don't know how..?

The code:

    app.route('/stations')
  .get(function (req, res) {
    // omitted
    res.setHeader('Access-Control-Allow-Origin', '*', 'Cache-Control', 'private, no-cache, no-store, must-revalidate');

    pool.query(`SELECT * FROM sel_alert_level s;`, function (error, result) {
      if (error)
      return res.status(500).json({ error: "Грешна заявка. Опитай отново !"})
      stations = result;
      res.json({ stations })
    });
  });

I try to put res.set({ 'content-type': 'application/json; charset=utf-8' }); before res.setHeader('Access-Control-Allow-Origin', '*', 'Cache-Control', 'private, no-cache, no-store, must-revalidate'); like this:

      app.route('/stations')
  .get(function (req, res) {
    // omitted
    res.set({ 'content-type': 'application/json; charset=utf-8' });
    res.setHeader('Access-Control-Allow-Origin', '*', 'Cache-Control', 'private, no-cache, no-store, must-revalidate');

    pool.query(`SELECT * FROM sel_alert_level s;`, function (error, result) {
      if (error)
      return res.status(500).json({ error: "Грешна заявка. Опитай отново !"})
      stations = result;
      res.json({ stations })
    });
  });

But this not work for me..

Upvotes: 1

Views: 443

Answers (1)

Andrey Popov
Andrey Popov

Reputation: 7510

You need to check the headers from the response (use dev tools if you're using the browser). JSON is UTF-8 by default and if you use express it should be fine, but just double check.

If that doesn't work - use res.set({ 'content-type': 'application/json; charset=utf-8' }); (Express again).

Upvotes: 1

Related Questions