I have this bind to my textfields onkeyup event:
function ajaxSearch(sstring,miszerint,startrow,Valid,notValid) {
setTimeout(function query(){
if (sstring.length <= 3)
{
$("#external").html("<p>min 3 chars please.</p>")
}
else
{
$('#loading').ajaxStart(function() {
$(this).show()
$("#external").hide()
});
$('#loading').ajaxComplete(function() {
$(this).hide()
$("#external").show()
});
$.ajax({
type:"GET",
url: "/myApp/getStd",
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
dataType:"application/x-www-form-urlencoded; charset=UTF-8",
data:"sstring="+escape(sstring)+"&options="+miszerint+"&startrow="+startrow+"&valid="+Valid+"¬Valid="+notValid+"&searchForm=1",
async: true,
success: function(data){
$("#external").html(data);
}
})
}
},1500)
}
The problem is that when I put hungarian chars (e.g., æőűúíéá”) into the textfield my servlet returns “�” instead of the provided char.
If I query the servlet directly without ajax it works fine.
In the JSP I have defined:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
plus
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
On the servlet I set the response charset to “UTF-8”. Any ideas?
You should use
encodeURIComponent()instead ofescape()for URL-encoding query parameters. Or, better, provide it as a JS object, then jQuery will worry about URL-encoding.