简体   繁体   中英

AJAX call to Java character encoding

I currently have 2 different jsp pages where both do calls to a java application. Currently in only one of them the character encoding works good.

Page A does:

$.ajax({
    url: _root + myPage,
    data: myData,
    success: mySuccessHandler,
    error: myErrorHandler});

Page B does:

$.post(_root + page, data, successHandler).error(errorHandler);

Inside the java application I'm using this code, there are 2 separate functions, but the only difference is the parameter name A uses "answerdata" and B uses "data":

request.setCharacterEncoding("UTF-8");
JSONObject data = new JSONObject(URLDecoder.decode(request.getParameter("answerdata"), "UTF-8"));

Both jsp pages have the data encapsulated inside a form:

<form id="answerdata" accept-charset="UTF-8" onsubmit="return false;">

Both also have this:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>

And only the page A, where it doesn't work has, but removing this doesn't have any effect:

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>

I've been through some pages on stackoverflow, but the only answer that worked for one of my pages so far was the

request.setCharacterEncoding("UTF-8")

But that only worked for page B

我能够使用以下方法修复它:

JSONObject data = new JSONObject(new String(request.getParameter("answerdata").getBytes(), "UTF-8"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM