2015-08-21 28 views
0

我有錯誤,當字符串「ああああ」變成了「????」然後將數據從servlet傳遞給javascript。值發送到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

確保:

  1. 爪哇文件被編碼爲MS932

  2. 的響應/請求被編碼爲SHIFT_JIS

  3. 前端HTML有元標記與字符集定義爲SHIFT_JIS

如果您使用的是Eclipse IDE,對於Java文件編碼,右鍵單擊文件名,在導航窗格中,單擊屬性。 Open Properties Dialog in Eclipse IDE

選擇 「文本文件編碼」>選擇 「其他」,然後鍵入MS932 Set Encoding

這將工作。 2年前,我在爲輝瑞日本公司工作,在開發階段的最初階段,我們面臨着這些挑戰。

+0

我找不到如何將Java文件編碼爲MS932。你能告訴我怎麼樣? –

+0

更新了我的答案,演示瞭如何設置Java文件的編碼 –

+0

它有幫助嗎? –