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("<开发者_如何学Pythonp>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 of escape()
for URL-encoding query parameters. Or, better, provide it as a JS object, then jQuery will worry about URL-encoding.
data: {
"sstring": sstring,
"options": miszerint,
"startrow": startrow,
"valid": Valid,
"notValid": notValid,
"searchForm": 1
}
Use UTF8 charset when you create your output stream in your servlet. Something like this:
resp.setCharacterEncoding("UTF-8");
OutputStream os = resp.getOutputStream();
BufferedWriter bos = new BufferedWriter(new OutputStreamWriter(os,"UTF8"));
精彩评论