使用离子框架的编码问题

Encoding issue using ionic-framework

本文关键字:编码 问题 框架      更新时间:2023-09-26

在我的应用程序中,我构建了一个json并将其发布到服务器

(....)
json = {"userID": idUser,  "name":$scope.data.name,
                          "type":AppService.getType().value, "race":$scope.data.race.name, "age":$scope.data.age}
(....)
$http.post(apiEndPoint+'/myapp/save', json)

当race.name有特殊字符时,我有问题。一切都是UTF-8。该值由服务器发送,我知道它是OK的,因为它正确地显示在UI上。当Ionic App把它发送回来时,它会把编码弄乱。

例如,当用户在界面上选择"testxxÃxx"时,服务器得到如下内容:

[userID:4, age:2342, name:Test, type:TEST, race:testxx??xx]

只是为了清楚,"testxxÃxx"是正确显示在UI上(在一个选择器上)。起初我以为这可能是Ionic View的限制,但事实并非如此。使用iOS模拟器时也会发生这种情况。

我很感激你的帮助!

您是否将HTTP POST请求的标头设置为UTF-8 ?

.config(["$httpProvider",
         function(httpProvider) {
              httpProvider.defaults.cache = false;
             httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
             return httpProvider['defaults']['transformRequest'] = function(
                     data) {
                 if (data == null) {
                     return data;
                 }
                 return $.param(data);
             };
         } ])