【发布时间】:2019-10-29 00:32:24
【问题描述】:
API 是使用 Nodejs Express 制作的。这个Hindi API 是现场直播,显示印地语的奇怪字符。 English version 完美显示英文结果。以下是印地语 API 的代码:
app.get("/api/find/hindi/:find",function(request, response)
{
let db = new sqlite3.Database("./quranDb.db",(err) => {
if (err){
console.log("Not connected to sqlite")
}
else{
console.log("Connected to sqlite")
}
});
let sql = `SELECT Surat_ID, Ayat_ID, Surat_Name, Hindi FROM QuranTest`;
db.all(sql, [], (err, rows) => {
if (err) {
throw err;
}
rows.forEach((row) => {
ayats.push(JSON.stringify({Translation: row.Hindi,SuratName: row.Surat_Name,SuratID: row.Surat_ID,AyatNo: row.Ayat_ID}));
});
//console.log(ayats);
Translation="";
Surat_No="";
Surah_Name="";
Ayat_No="";
try {
ayats.forEach(function(element) {
if (element.toLowerCase().includes(request.params.find.toLowerCase())===true)
{
counting++;
element=JSON.parse(element);
Surah_Name = element.SuratName;
Ayat_No = element.AyatNo;
Surah_No = element.SuratID
Translation = "In Surah "+ element.SuratName+", Ayat Number: "+element.AyatNo+", Quran says: "+ element.Translation;
const tempObj={
Surah_No,
Surah_Name,
Ayat_No,
Translation
}
parentObj[`result_${counting}`]=tempObj
}
if (counting===10){
throw BreakException
}
})
} catch(e) {
if (e!==BreakException) throw e
}
if (counting ===0){
response.write(JSON.stringify({"speech":"No results found"}))
}
else{
response.write(JSON.stringify(parentObj))
}
response.send();
counting = 0;
parentObj={};
});
empty();
function empty() {
ayats.length = 0;
}
db.close((err) => {
if (err) {
return console.error(err.message);
}
console.log('Close the database connection.');
});
})
印地语 API
可以看出,translation 字段的值是乱码。它应该是印地语格式。 SQL 数据库没有任何问题。它显示了完美的印地语。
我也试过
...
}
else{
response.write(JSON.stringify(parentObj))
}
response.writeHead(200, {"Content-Type": "text/html; charset=utf-8"});
response.send();
counting = 0;
...
但它会阻止印地语 api 执行。浏览器返回错误刷新页面等。
【问题讨论】:
标签: node.js sqlite express utf-8 hindi