【问题标题】:Encoding issue using ionic-framework使用离子框架的编码问题
【发布时间】:2015-08-09 19:09:13
【问题描述】:

在我的应用程序中,我构建了一个 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。 该值由服务器发送,我知道这没问题,因为它正确显示在 UI 上。 当 Ionic App 将其发回时,它会混淆编码。

例如,当用户在界面选择“testxxÃxx”时,服务器会得到这样的结果:

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

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

感谢您的帮助!

【问题讨论】:

    标签: javascript ios node.js ionic-framework ionic-view


    【解决方案1】:

    您是否将 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);
                 };
             } ])
    

    【讨论】:

    • 好主意。我试过: $httpProvider.defaults.headers.post['Content-Type'] = 'application/json;charset=utf-8';但它不起作用。同样的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-12
    • 2015-10-28
    • 2020-03-07
    相关资源
    最近更新 更多