Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在节点js+中响应json TIS-620到UTF-8;速递(API)_Json_Node.js_Api_Express_Utf 8 - Fatal编程技术网

如何在节点js+中响应json TIS-620到UTF-8;速递(API)

如何在节点js+中响应json TIS-620到UTF-8;速递(API),json,node.js,api,express,utf-8,Json,Node.js,Api,Express,Utf 8,我在使用tis-620获取语言数据时遇到问题 [MySQL结构] CREATE TABLE `foo` ( `id` int(11) NOT NULL, `name` varchar(20) DEFAULT NULL, `address` varchar(60) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=tis620 [MySQL数据] | id | name | addres

我在使用tis-620获取语言数据时遇到问题

[MySQL结构]

CREATE TABLE `foo` (
  `id` int(11) NOT NULL,
  `name` varchar(20) DEFAULT NULL,
  `address` varchar(60) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=tis620
[MySQL数据]

| id |   name   |    address    |
--------------------------------|
|  1 | ทดสอบ   | 6/12 บ้านแสนดี  |
[节点功能]

function getAllFoo() {
    return new Promise((resolve, reject) => {
        try {

            // Retrieving
            db.query("SELECT * FROM foo", (err, rows, fields) => {
                if(err) { throw err }
                resolve(rows)
            })

        } catch (error) {
            reject(error)
        }
    })
}
[nodejsapi]

const Foo = require('./foo')

app.get('/getFoo', (req, res) => {

    Foo.getAllFoo().then((rows) => {

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

     }).catch((err) => {
         throw err
     })

    res.status(200).json(rows);
})
然后,我尝试请求该端点
/getFoo
,但响应数据是:��ҹ������

[响应json数据]

[
  {
    "id" : 1,
    "name" : "��ҹ.������",
    "address" : "��ҹ������ �.� �.������"
  }
]
此JSON结果将用于我的PHP项目解决方案:

[MySQL连接] -在mysql连接中,将
字符集添加到tis620,并在数据库连接时添加
“SET NAMES UTF8”

var mysql      = require('mysql');

var connection = mysql.createConnection({

  host     : 'example.org',
  user     : 'bob',
  password : 'secret'
  charset  : 'tis620'

})

connection.connect(function(err) {

  if (!err) {

    connection.query("SET NAMES UTF8")

    console.log('connected as id ' + connection.threadId);

  } else {     

    console.error('error connecting: ' + err.stack);        

  }

});

可持续的解决方案是在任何地方使用UTF-8。