0

在我的应用程序中,我构建了一个 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 应用程序将其发回时,它会混淆编码。

例如,当用户在界面选择“testxxÃxx”时,服务器会得到如下信息:

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

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

我感谢您的帮助!

4

1 回答 1

0

您是否将 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);
             };
         } ])
于 2015-05-27T16:12:04.293 回答