Showing posts with label AJAX. Show all posts
Showing posts with label AJAX. Show all posts

AJAX AutoSuggest with Parameters in Greeks

Usage of the following control:
http://www.brandspankingnew.net/archive/2006/08/ajax_auto-suggest_auto-complete.html

Some changes in Javascript bsn.AutoSuggest_2.1.3_comp.js in order to support Greek language.

Replace twice the
encodeURIComponent(this.sInp)
with
encodeURI(encodeURI(this.sInp))

Read the greek parameters using in a java bean the following code:
try {
request.setCharacterEncoding("UTF-8");
nm = request.getParameter("nm");
nm = java.net.URLDecoder.decode (nm, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

Struts 1.x + Ajax + Greek Language (UTF-8)

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();
}