7

I have a web page that requests an hebrew string using Ajax but the string is returned as '??????'

The weird thing is that when inserting that same string to the page using JSTL and not Ajax, it is shown correctly...

In my web page I'm declaring

<%@ page contentType="text/html" pageEncoding="UTF-8"%>

That's my controller:

@RequestMapping("get_label")   
public @ResponseBody String getLabel()
{
   String str = "בדיקה";

   return str;
}

And my ajax request:

$.ajax({
    url:    "get_label",
    success:    function(result)
    {
        alert(result);
        $("#parameter_select label").text(result);
    }
});

Any ideas what am I doing wrong here?

1 Answer 1

15

This happens because AJAX-calls by default use browser's default encoding (f.e. ANSI). For overriding this you need to do:

jQuery style - mimeType:

$.ajax({
    url:    "get_label",
    mimeType:"text/html; charset=UTF-8",
    success:    function(result)
    {
        alert(result);
        $("#parameter_select label").text(result);
    }
});

Vanilla JS style:

xhr.overrideMimeType("text/html; charset=UTF-8")

But from the other hand you need to be sure, that server also returns appropriate response. For this you need to check the following:

  1. Add UTF-8 support for web-container (i.e. Tomcat) with adding URIEncoding="UTF-8" for your Connector settings in server.xml; check this for more information.
  2. If previous change didn't help (though it has to), please also make sure, that servlet response's character set is also UTF-8.

For this you can use either explicit call of method:

@RequestMapping("get_label")
public @ResponseBody String getLabel(HttpServletResponse response)
{
    String str = "בדיקה";

    //set encoding explicitly
    response.setCharacterEncoding("UTF-8");

    return str;
}

Or, which seems to be more preferable for @ResponseBody and Spring 3.1+:

@RequestMapping(value = "get_label", produces = "text/html; charset=UTF-8")
public @ResponseBody String getLabel(HttpServletResponse response)
{
    String str = "בדיקה";

    return str;
}

As a conclusion I would like to clarify, that for proper handling of AJAX-calls with UTF-8 encoding, you have to make sure, that:

  • web-container supports this properly
  • response's character encoding is UTF-8
  • AJAX request character encoding is also UTF-8
Sign up to request clarification or add additional context in comments.

5 Comments

For testing purposes please consider clearing browser's caches as JS is cached quite well.
If it didn't help, then issue should be on the server-side. In this case could you please provide: 1) your spring version; 2) your web-container
I'm using spring version 3.1.3 and Tomcat 7
Amazing! It works! Thanks :) I added - produces = "text/html; charset=UTF-8" - and that did it
Great, that I managed to help you. But in any case, I would suggest not remove at least AJAX part with mimeType

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.