I am passing values to server using encodeURIComponent
through an AJAX call, but on the server side request.getParameter()
is not decoding it automatically. How can I decode the value? I am using spring MVC.
$.ajax({
type: "POST",
url: "test/editTest.do",
data: "id=" + encodeURIComponent(id)
})
Any help and guidance would be appreciated
Thanks
Add a charset filter as a first filter in your web.xml
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Also, you can make sure that your server is configured properly, for tomcat for instance adding URIEncoding to connector
<connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URIEncoding="UTF-8"/>
will specify the character encoding used to decode the URI. If you're not using tomcat you should find an equivalent for your server