Spring: Ajax call to encode params @ResponseBody not working in IE

I am working with Spring and trying to make an ajax call to @ResponseBody in my controller.

UPDATE

Ok, I added the changes that were told to me in my ajax settings. My parameter "jtSearchParam" still has the same encoding issue in IE. + I got another error, 406, the response header has the wrong content type.

Here is my new code

Controller:

@RequestMapping(method = RequestMethod.POST, consumes="application/json; charset=utf-8", produces="application/json; charset=utf-8")
    public @ResponseBody JSONObject getUsers(@RequestParam int jtStartIndex, @RequestParam int jtPageSize,
            @RequestParam String jtSorting, @RequestParam String jtSearchParam,
            HttpServletRequest request, HttpServletResponse response) throws JSONException{

        Gson gson = new GsonBuilder()
                .setExclusionStrategies(new UserExclusionStrategy())
                .create();

        List<User> users = userService.findUsers(jtStartIndex ,jtPageSize, jtSorting, jtSearchParam);
        Type userListType = new TypeToken<List<User>>() {}.getType();

        String usersJsonString = gson.toJson(users, userListType);
        int totalRecordCount = userDao.getAmountOfRows(jtSearchParam);

        usersJsonString = "{\"Message\":null,\"Result\":\"OK\",\"Records\":" + usersJsonString + ",\"TotalRecordCount\":" + totalRecordCount + "}";

        JSONObject usersJsonObject = new JSONObject(usersJsonString);

        return usersJsonObject;
    }

So, as you can see, I set the content type to produces, but that doesn't help. If I debug the response header, it looks like this: (This makes it impossible to use 406 from the browser)

response header

And my new ajax settings:

...
headers: { 
                 Accept : "application/json; charset=utf-8",
                "Content-Type": "application/json; charset=utf-8"
            },
            contentType: "application/json; charset=utf-8",
            mimeType:"application/json; charset=UTF-8",
            cache:false,
            type: 'POST',
            dataType: 'json'
...

And my options still look the same in IE!

IE debugged values

+5
source
4

, json :

ResponseEntity , , ajax json-, 406 Http-.

@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<String> getUsers(@RequestParam int jtStartIndex, @RequestParam int jtPageSize,
        @RequestParam String jtSorting, @RequestParam String jtSearchParam,
        HttpServletRequest request, HttpServletResponse response) throws JSONException{

    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.add("Content-Type", "application/json; charset=utf-8");

    Gson gson = new GsonBuilder()
            .setExclusionStrategies(new UserExclusionStrategy())
            .create();

    List<User> users = userService.findUsers(jtStartIndex ,jtPageSize, jtSorting, jtSearchParam);
    Type userListType = new TypeToken<List<User>>() {}.getType();

    String usersJsonString = gson.toJson(users, userListType);
    int totalRecordCount = userDao.getAmountOfRows(jtSearchParam);

    usersJsonString = "{\"Message\":null,\"Result\":\"OK\",\"Records\":" + usersJsonString + ",\"TotalRecordCount\":" + totalRecordCount + "}";

    return new ResponseEntity<String>(usersJsonString, responseHeaders, HttpStatus.OK);
}

:

IE "ü, ä ..", , URL- : "jtSearchParam = wü", : "jtSearchParam = w% C3% BC" ( , IE)

, URL-, JavaScript encodeURI , URL- :

encodeURI(jtSearchParam)

+5

, json

dataType: 'json'

contentType: "text/html; charset=utf-8"

json application/json messageConverters json json, java- json , @ResponseBody String @ResponseBody User, User - pojo bean .

+3

, :

  • - , UTF-8
  • CharacterEncodingFilter

CharacterEncodingFilter , Spring. web.xml.

<filter>
    <filter-name>encodingfilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>encoding</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>

</filter>

<filter-mapping>
    <filter-name>encodingfilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

GET Tomcat, , Connector URIEncoding="utf-8". .

JSON

, Jackson Mapper @ResponseBody . Message, JSON. :

   public @ResponseBody Message getUsers(int jtStartIndex, jtPageSize, String jtSorting, String jtSearchParam) {

      List<User> users = userService.findUsers(jtStartIndex ,jtPageSize, jtSorting, jtSearchParam);
      int totalRecordCount = userDao.getAmountOfRows(jtSearchParam);

      Message message = new Message();
      message.setRecords(users);
      message.setTotalRecordCount(totalRecordCount);

      return message;
  }

@RequestParam, , , .

If you use jQuery, it hardly matters what the actual content-typeanswer is if the content can be successfully parsed as JSON. Use dataType: 'json', however, to prevent jQuery from guessing incorrectly.

content-typematters, of course, if you use produces. If you do not need this to narrow down your query mappings, I suggest getting rid of it.

+2
source

I would check that it calls your json method, since there may be another similar method returning text / html.

0
source

All Articles