字符串“ああああ”变为“ ????”时出现错误然后将数据从servlet传递到javascript。

控制器.java

@RequestMapping(value = "searchbook", method = RequestMethod.POST, produces = "text/plain")
    public @ResponseBody String myController(HttpServletRequest request) throws SQLException {
        String myItem = request.getParameter("searchid");

        PostgrConnect db = new PostgrConnect();
        ResultSet rs;
        Book book = new Book();

        try {
            rs = db.getData("select * from mt_book where book_id='" + myItem + "'");
            while (rs.next()) {
                book.setBook_id(rs.getString("book_id"));
                book.setBook_title(rs.getString("book_title"));
                book.setAuthor_name(rs.getString("author_name"));
                book.setPublisher(rs.getString("publisher"));
                book.setPublication_day(rs.getString("publication_day"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e);
        }

        return book.getBook_id() + ";" + book.getBook_title() + ";" + book.getAuthor_name() + ";" + book.getPublisher()
                + ";" + book.getPublication_day();
    }


javascript

function Search() {
    if (checkInputSearch() == "true") {
    var date = "";
    var searchid = document.getElementById("bookid").value;
    $.ajax({
        type : "POST",
        url : "searchbook",
        data : {
        searchid : searchid
        },
        success : function(html) {
        var res = html.split(";");
        if (res[0] == "null") {
            alert(MSG0000 + " " + bookid.value + " " + MSG0004);
            Clear();
        } else {
            alert(MSG0003);
            document.getElementById("bookid").value = res[0];
            document.getElementById("booktitle").value = res[1];
            document.getElementById("authorname").value = res[2];
            document.getElementById("publishher").value = res[3];
            date = res[4].split("-");
            document.getElementById("day").value = date[2];
            document.getElementById("month").value = date[1];
            document.getElementById("year").value = date[0];
        }
        },
        error : function(e) {
        alert(MSG0005);
        console.log("Error:" + e);
        }
    });
    }

}


我可以从数据库中获取数据:

http://imgur.com/fpibNPt

但是当传递给javascript时:

http://imgur.com/WhGwKwP

最佳答案

确保以下各项:


Java文件编码为MS932
响应/请求编码为Shift_JIS
前端HTML的meta标签定义了charset为Shift_JIS


如果您使用的是Eclipse IDE,则对于Java文件编码,请在导航窗格中右键单击文件名,然后单击“属性”。
javascript - 将值从servlet发送到javascript错误(日语字符)-LMLPHP

选择“文本文件编码”>选择“其他”,然后键入MS932
javascript - 将值从servlet发送到javascript错误(日语字符)-LMLPHP

这将起作用。
2年前,我在辉瑞日本公司工作,在开发阶段的开始,我们就面临这些挑战。

10-06 07:45