samedi 9 mai 2015

AJAX: encoding data obtained by ajax

I have a database with tables in UTF-8. Spring web application connects to a database at jdbc:mysql://localhost:3306/recr?zeroDateTimeBehavior=convertToNull&amp;characterEncoding=utf8&amp;characterSetResults=utf8 All java and jsp files in UTF-8. In header jsp files has a <%@ page contentType="text/html; charset=UTF-8" %>

The page is displayed correctly, but when received by the Ajax data from the server, they are displayed as ?????

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);

                  }
              });

Controller

@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;

}

What could be the problem?

Aucun commentaire:

Enregistrer un commentaire