【问题标题】:AJAX: encoding data obtained by ajaxAJAX:通过ajax获取的编码数据
【发布时间】:2015-07-20 06:01:29
【问题描述】:

我有一个包含 UTF-8 表的数据库。 Spring Web 应用程序连接到jdbc:mysql://localhost:3306/recr?zeroDateTimeBehavior=convertToNull&characterEncoding=utf8&characterSetResults=utf8 的数据库 UTF-8 格式的所有 java 和 jsp 文件。在头jsp文件中有一个 <%@ page contentType="text/html; charset=UTF-8" %>

页面显示正确,但是当从服务器接收到Ajax数据时,显示为?????

javascript

$(document).ready(function() {
        $("#b2").click(function(){
              $.ajax({
                  type: "GET",
                  url: "/recr/getRegion2",
                  data: 'id=3159',
                  contentType: "application/json; charset=UTF-8",
                  dataType: "json",
                  success: function (data) {
                        //var json = jQuery.parseJSON(data);
                        for(var x in data){
                            $('#region').append($('<option>').text(data[x]).attr('value', x));


                            }

                        alert("Data: " + data);
                  },
                  error: function (errormessage) {

                    alert("error" + errormessage);

                  }
              });

控制器

@RequestMapping(value = "/getRegion2", method = RequestMethod.GET)
public @ResponseBody String getRegion2(@RequestParam("id") long id) throws Exception {
    System.out.println("пришло id : " + id);
    List<Region> regions = Facade.getRegionDao(dataSource).getAllRegionsByCountry(id);
    String res = JsonTransformer.transformRegionList(regions);
    return res;

}

可能是什么问题?

【问题讨论】:

  • 您的jdbc 网址似乎包含&amp;amp;,这是不应该的。只是猜测。
  • &amp;amp; - 保留字符 xml,因此使用 &amp;amp;

标签: java mysql ajax spring utf-8


【解决方案1】:

我找到了解决方案。 必须在控制器映射中指定。

@RequestMapping(value = "/getRegion2", produces={"application/json; charset=UTF-8"},method = RequestMethod.GET)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-10
    • 1970-01-01
    • 2013-04-12
    • 2018-06-04
    • 2018-06-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多