簡體   English   中英

將值從servlet發送到javascript錯誤(日語字符)

[英]send value from servlet to javascript error (japanese character)

字符串“ああああ”變為“ ????”時出現錯誤 然后將數據從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

確保以下各項:

  1. Java文件編碼為MS932

  2. 響應/請求編碼為Shift_JIS

  3. 前端HTML的meta標簽定義了charset為Shift_JIS

如果您使用的是Eclipse IDE,則對於Java文件編碼,請在導航窗格中右鍵單擊文件名,然后單擊“屬性”。 在Eclipse IDE中打開屬性對話框

選擇“文本文件編碼”>選擇“其他”,然后鍵入MS932 設置編碼

這將起作用。 2年前,我在輝瑞日本公司工作,在開發階段的開始,我們就面臨這些挑戰。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM