Requête AJAX via jQuery mal décodée
Bonsoir à tous,
j'ai un petit souci lorsque j'essaie de mettre en place des requêtes AJAX sur un projet web : la requête transmise semble correcte, pourtant, l'encodage de la chaîne reçue semble être corrompu.
Voici ma Config :
web.xml :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
| <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_4.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_4.xsd"
id="Test" version="2.4">
<display-name>Ajax Test</display-name>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.xml</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>10000</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/webappContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>spring</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/webappContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/static/*</url-pattern>
</servlet-mapping>
<filter>
<filter-name>httpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>httpMethodFilter</filter-name>
<servlet-name>spring</servlet-name>
</filter-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app> |
webappContext.xml :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| <?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="fr.test" />
<mvc:annotation-driven />
<bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
</map>
</property>
</bean>
<bean name="jsonView" class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
</beans> |
index.jsp :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta name="language" content="fr" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="/test/static/js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="/test/static/js/test.js" charset="UTF-8"></script>
</head>
<body>
<form name="testform" method="post">
<textarea id="testarea"></textarea>
<input type="button" id="testbutton" value="Submit AJAX" />
</form>
</body>
</html> |
test.js :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
| function checkEncoding(textStr)
{
$.ajax({url: "ajax/test-encoding", dataType: "json", data: { text: textStr }, success : function(data, status, req) { $("#testarea").val(data.result); }});
}
$(document).ready
(
function()
{
$.ajaxSetup({ scriptCharset: "utf-8" , contentType: "application/json; charset=utf-8"});
$("#testarea").val("Entrez le texte à envoyer via AJAX ici").click(function(e){ if ($(this).val() == "Entrez le texte à envoyer via AJAX ici") { $(this).val(""); } });
$("#testbutton").click(function(e) { checkEncoding($("#testarea").val().trim()); });
}
); |
Mon contrôleur :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
| @Controller
@RequestMapping("/ajax")
public class TestController
{
private static final Logger log = Logger.getLogger(TestController.class);
@RequestMapping(value = "test-encoding", method = RequestMethod.GET)
public @ResponseBody JsonBean testEncoding(@RequestParam String text)
{
log.debug(String.format("Received text \"%s\"", text));
return new JsonBean(text);
}
} |
et enfin mon JsonBean :
Code:
1 2 3 4 5 6 7 8 9
| public class JsonBean implements Serializable
{
private static final long serialVersionUID = 1L;
private String result;
public JsonBean(String result) { this.result = result; }
public String getResult() { return this.result; }
public void setResult(String result) { this.result = result; }
} |
Le fonctionnement est très simple :
- Quand je lance mon appli, la page index.jsp s'affiche. Elle contient un champ texte ainsi qu'un bouton.
- Un clic sur le bouton déclenche un appel Ajax GET avec jQuery, qui envoie simplement le contenu du champ texte. Au passage, le contenu du champ texte est URL-encodé.
- Le contrôleur reçoit la chaîne décodée par Spring, mais mal.
En effet, lorsque j'envoie "purée" par exemple, la requête ajax suivante est envoyée : http://localhost/test/ajax/test-enco...ext=pur%C3%A9e
Pourtant, en mettant un point d'arrêt dans mon contrôleur, je vois bien que je reçois la chaîne : "purée"
Est-ce que quelqu'un sait pourquoi la chaîne est décodée en ISO ?
Merci pour vos réponses...