Node.js AngularJS,通过服务器从Json获得的特殊字符
我正在尝试使用NodeJS版本10.15.1、AngularJS版本1.5.8和UTF8编码的html创建一个小型多语言项目。我应该继续使用自己的功能,而不是使用其他模块Node.js AngularJS,通过服务器从Json获得的特殊字符,node.js,json,angularjs,special-characters,Node.js,Json,Angularjs,Special Characters,我正在尝试使用NodeJS版本10.15.1、AngularJS版本1.5.8和UTF8编码的html创建一个小型多语言项目。我应该继续使用自己的功能,而不是使用其他模块 我创建了两个不同的json文件,其中包含两种不同的语言。json通过服务器使用$http调用加载,答案存储在$scope变量中 $http.post(apihost + '/languages/language_frontend', {page: "home"}).then(function(languag
我创建了两个不同的json文件,其中包含两种不同的语言。json通过服务器使用
$http
调用加载,答案存储在$scope
变量中
$http.post(apihost + '/languages/language_frontend', {page: "home"}).then(function(language) {
$scope.language = language.json;
});
我通过参数page
来过滤函数应该检索的json的一部分
router.post('/language_frontend', function(req, res, next) {
return new Promise(function(resolve,reject) {
if(config.language == 'it') return res.json({status: 'ok', json: italian_frontend[req.body.page]});
else if(config.language == 'en') return res.json({status: 'ok', json: english_frontend[req.body.page]});
});
});
这是json的一部分
{
"home": {
"planning": "Pianificazione",
"activities_planning": "Pianificazione Attività"
},
"login": {
"test_one": "italiano uno",
"test_one": "italiano due"
}
}
这是显示信息的html
<div class="panel-heading">
<div class="row">
<div class="col-xs-3"><i class="fa fa-mobile-phone fa-5x"></i></div>
<div class="col-xs-9 text-right">
<div class="huge ng-binding">{{language.activities_planning}}</div>
</div>
</div>
</div>
{{语言.活动规划}
问题是活动\u planning
的显示带有重音字符,来自服务器端调用,我不知道如何正确显示。我希望有一个通用的解决方案可以在任何地方实现,所以我不必担心有一些特殊字符的异常
这是没有解决方案的结果:Pianificazione Attivit�代码>
有什么建议吗?好的,给你。我试着和你做同样的事情:
在后端的我的app.js
中,我获取了JSON文件的内容,并将其暴露在/language
路径中:
const path = require("path");
const express = require("express");
const app = express();
const language = require("./test.json");
app.use('/', express.static(path.join(__dirname, 'public')));
app.get('/language', (req, res) => res.json({ status: "ok", json: language }));
app.listen(5000, function() {console.log("Server is running on port 5000")});
在客户端的myindex.js
中,我向服务器发送请求以获取JSON文件:
angular.module("app", []).controller("MyController", ["$scope", "$http",
function ($scope, $http) {
// send request to get the json
$http.get('/language').then(function (resp) {
const language = resp.data.json;
console.log(language); // I've checked on the console.log, the text is OK
$scope.text = language.test; // bind to screen
});
}
]);
在我的index.html
中,我只使用它:
<body ng-app="app">
<div ng-controller="MyController">
<h1>Hello {{text}}!</h1>
</div>
</body>
你好{{text}}!
我所拥有的:
我试图复制相同的代码,但我对特殊字符没有任何问题。可能问题出在代码的其他地方。如果你愿意,我可以上传我的工作版本。@ungkhoaĐinh是的,请……我还是不明白,如果有任何帮助,我们将不胜感激,谢谢!