首页 > 解决方案 > 如何在 json 响应中使用 utf-8 格式的数据

问题描述

我试图以不同的语言保存数据。它已保存,但是当我尝试使用 AJAX 获取数据时,它没有提供正确的 JSON 数据。

当我用英语保存数据时,我得到了正确的响应。当我用其他语言保存数据时,我遇到了这个问题。我该如何解决这个问题?谁能帮帮我吗。

$.ajax({
  type: paramsObj.type, //mandatory field
  url: https://blabla.com/asset/items.json_ajax=true
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
    'Cache-Control': 'no-cache',
    'Authorization': auth
  },
  async: true,
  data: paramsObj.data,
  dataType: 'json',
  cache: false,
  timeout: 120000,
  success: function(responseData) {
    paramsObj.success(responseData); //Invoke success callback method.
  },
  error: function(jqXHR, textStatus, errorThrown) {
    console.log("Error : " + textStatus);
    console.log(jqXHR.responseText);

  }
});

在 Java 控制器中处理数据后,我将数据推送到 JSON 对象并像这样返回

OutputStream out = null;
String jsonData = jsonObj.toString();
try {
      out = response.getOutputStream();
      response.setContentLength(jsonData.length());
      response.setHeader("Access-Control-Allow-Origin", "*");
      response.setHeader("Access-Control-Allow-Headers", "Content-Type");
      response.setHeader("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS");
      out.write(jsonData.getBytes());
      out.flush();
} catch (Exception ex) {
      ex.printStackTrace();
}

在控制器端,JSON 对象是这样的:

{
  "categoryItems": {
    "Laundry": [{
      "id": 16640,
      "name": "చొక్కా",
      "shortDescription": "",
      "salesRate": 80,
      "image": "",
      "igst": 0,
      "cgst": 0,
      "sgst": 0,
      "state": "ACTIVE",
      "value0": "1",
      "value1": "No",
      "value2": "",
      "productposition": 0,
      "type": "Goods",
      "stockInHand": 0,
      "tags": "salesInfo",
      "catalog": "Laundry Catalog",
      "catalogCategory": "Laundry",
      "categoryId": 1979,
      "color": "#73ebdb ",
      "txtColor": "#000000",
      "groupId": 0
    }, {
      "id": 16641,
      "name": "తువాలు",
      "shortDescription": "",
      "salesRate": 40,
      "image": "",
      "igst": 0,
      "cgst": 0,
      "sgst": 0,
      "state": "ACTIVE",
      "value0": "1",
      "value1": "No",
      "value2": "",
      "productposition": 0,
      "type": "Goods",
      "stockInHand": 0,
      "tags": "salesInfo",
      "catalog": "Laundry Catalog",
      "catalogCategory": "Laundry",
      "categoryId": 1979,
      "color": "#73ebdb ",
      "txtColor": "#000000",
      "groupId": 0
    }, {
      "id": 16642,
      "name": "Curtain Door",
      "shortDescription": "",
      "salesRate": 0,
      "image": "",
      "igst": 0,
      "cgst": 0,
      "sgst": 0,
      "state": "ACTIVE",
      "value0": "1",
      "value1": "No",
      "value2": "",
      "productposition": 0,
      "type": "Goods",
      "stockInHand": 0,
      "tags": "",
      "catalog": "Laundry Catalog",
      "catalogCategory": "Laundry",
      "categoryId": 1979,
      "color": "#73ebdb ",
      "txtColor": "#000000",
      "groupId": 0
    }]
  },
  "categoryIds": [1978, 1979, 1980, 1981, 1982],
  "items": [],
  "groupCategoryItems": [],
  "groupItems": [],
  "taxes": []
}

然而,JSON 响应是这样的:

{
  "categoryItems": {,
    "Laundry": [{
      "id": 16640,
      "name": "à °šà ±Šà °•à ±Âà °•à °¾",
      "shortDescription": "",
      "salesRate": 80,
      "image": "",
      "igst": 0,
      "cgst": 0,
      "sgst": 0,
      "state": "ACTIVE",
      "value0": "1",
      "value1": "No",
      "value2": "",
      "productposition": 0,
      "type": "Goods",
      "stockInHand": 0,
      "tags": "salesInfo",
      "catalog": "Laundry Catalog",
      "catalogCategory": "Laundry",
      "categoryId": 1979,
      "color": "#73ebdb ",
      "txtColor": "#000000",
      "groupId": 0
    }, {
      "id": 16641,
      "name": "Towel, L",
      "shortDescription": "",
      "salesRate": 40,
      "image": "fk_download",
      "igst": 0,
      "cgst": 0,
      "sgst": 0,
      "state": "ACTIVE",
      "value0": "1",
      "value1": "No",
      "value2": "",
      "productposition": 0,
      "type": "Goods",
      "stockInHand": 0,
      "tags": "salesInfo",
      "catalog": "Laundry Catalog",
      "catalogCategory": "Laundry",
      "categoryId": 1979,
      "color": "#73ebdb ",
      "txtColor": "#000000",
      "groupId": 0
    }, {
      "id": 16642,
      "name": "Curtain Door",
      "shortDescription": "",
      "salesRate": 0,
      "image": "",
      "igst": 0,
      "cgst": 0,
      "sgst": 0,
      "state": "ACTIVE",
      "value0": "1",
      "value1": "No",
      "value2": "",
      "productposition": 0,
      "type": "Goods",
      "stockInHand": 0,
      "tags": "",
      "catalog": "Laundry Catalog",
      "catalogCategory": "Laundry",
      "categoryId": 1979,
      "color": "#73ebdb ",
      "txtColor": "#000000",
      "groupId": 0
    }]
  },
  "categoryIds": [1978, 1979, 1980, 1981, 1982],
  "items": [],
  "groupCategoryItems": [],
  "groupItems": []

我将控制器端代码更改为:

OutputStream out = null;
String jsonData = jsonObj.toString();
try {
      out = response.getOutputStream();
      response.setContentLength(jsonData.length());
      response.setHeader("Access-Control-Allow-Origin", "*");
      response.setHeader("Access-Control-Allow-Headers", "Content-Type");
      response.setHeader("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS");
      response.setContentType("application/json");
      response.setCharacterEncoding("UTF-8");
      out.write(jsonData.getBytes());
      //System.out.println("---------------------");
      //System.out.println(out.toString());
      out.flush();
} catch (Exception ex) {
      ex.printStackTrace();
}

但是,我得到一个不完整的 JSON 响应

{
  "categoryItems": {
    "Dry Cleaning": [],
    "Laundry": [{
      "id": 16640,
      "name": "చొక్కా",
      "shortDescription": "",
      "salesRate": 80,
      "image": "",
      "igst": 0,
      "cgst": 0,
      "sgst": 0,
      "state": "ACTIVE",
      "value0": "1",
      "value1": "No",
      "value2": "",
      "productposition": 0,
      "type": "Goods",
      "stockInHand": 0,
      "tags": "salesInfo",
      "catalog": "Laundry Catalog",
      "catalogCategory": "Laundry",
      "categoryId": 1979,
      "color": "#73ebdb ",
      "txtColor": "#000000",
      "groupId": 0
    }, {
      "id": 16641,
      "name": "తువాలు",
      "shortDescription": "",
      "salesRate": 40,
      "image": "",
      "igst": 0,
      "cgst": 0,
      "sgst": 0,
      "state": "ACTIVE",
      "value0": "1",
      "value1": "No",
      "value2": "",
      "productposition": 0,
      "type": "Goods",
      "stockInHand": 0,
      "tags": "salesInfo",
      "catalog": "Laundry Catalog",
      "catalogCategory": "Laundry",
      "categoryId": 1979,
      "color": "#73ebdb ",
      "txtColor": "#000000",
      "groupId": 0
    }, {
      "id": 16642,
      "name": "Curtain Door",
      "shortDescription": "",
      "salesRate": 0,
      "image": "",
      "igst": 0,
      "cgst": 0,
      "sgst": 0,
      "state": "ACTIVE",
      "value0": "1",
      "value1": "No",
      "value2": "",
      "productposition": 0,
      "type": "Goods",
      "stockInHand": 0,
      "tags": "",
      "catalog": "Laundry Catalog",
      "catalogCategory": "Laundry",
      "categoryId": 1979,
      "color": "#73ebdb ",
      "txtColor": "#000000",
      "groupId": 0
    }],
    "Pressed/Iron": [],
    "Alteration": [],
    "Repairs": []
  },
  "categoryIds": [1978, 1979, 1980, 1981, 1982],
  "items": [],
  "groupCategoryItems": [],
  "gr

标签: javajqueryjsonajax

解决方案


你有没有试过这个:

@RequestMapping(value = "/YOUR_URL_NAME", method = RequestMethod.POST, produces = "application/json; charset=utf-8");

推荐阅读