In order to support UTF-8 (greek characters) in the parameters of a URI with AJAX, you have to do the following steps:
1. In the AJAX .js file, the function that requests data from the server...
function getDataFrom(str){
str = trim(str);
if ( str.length==0 ) {
document.getElementById(your_id).value = "";
return;
}
// 1. Create the XmlHttp Object
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null){
alert ("Your browser does not support AJAX!");
return;
}
var url="your_struts_url_action.do?do=a&nm=" + str;
// SOS - Call twice the encoding function...
url = encodeURI (url);
url = encodeURI (url);
// 2. Event handler when readystate changes....
xmlHttp.onreadystatechange=your_function;
// 3. Request info from the db
xmlHttp.open("GET",url,true);
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
xmlHttp.send(null);
}
Step 2: In the Action that you call from the AJAX function above, read the parameter and decode it:
try {
request.setCharacterEncoding("UTF-8");
name = request.getParameter("nm");
name = java.net.URLDecoder.decode ( sYphresia, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
No comments:
Post a Comment