2013-01-11 26 views
0

我有一個HTML頁面形式,而下面的代碼:解碼參數從阿賈克斯後以專字符

$.ajax({ 
    url: 'xxxxx', 
    data: $('#contact-form').serialize(), 
    type: 'post', 
    cache: false, 
    dataType: 'html', 
    success: function (data) {....}, 
    error: contact.error 
}); 

當我通過郵局從HTML頁面發送一個字符串,例如,字符串在Java中,我收到字符串áéíúó。 我該怎麼做才能解決這個問題?任何幫助將非常大膽。

(日期20131401) 解決方案: @TechSpellBound,感謝您的回覆。您的解決方案促使我尋找另一:d,以及我發現了兩個解決方案,爲我工作: 1)在Java中,使用下面的代碼:

String param = new String(req.getParameter("param").getBytes(), "iso-8859-1"); 

2)其他溶液,在我的Tomcat配置文件web.xml和放:

<filter> 
     <filter-name>setCharacterEncodingFilter</filter-name> 
     <filter-class>org.apache.catalina.filters.SetCharacterEncodingFilter</filter-class> 
      <init-param> 
       <param-name>encoding</param-name> 
       <param-value>UTF-8</param-value> 
      </init-param> 
     <async-supported>true</async-supported> 
</filter> 

<filter-mapping> 
    <filter-name>setCharacterEncodingFilter</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 

隨後的server.xml文件,把財產的URIEncoding = 「UTF-8」 在 配置的所有連接器再次感謝您。

+0

是你的html頁面UTF-8,它應該是這樣嗎?你是否也在服務器端使用utf-8? ajax調用總是utf-8。 – eis

+0

我們需要看到更多的代碼才能夠提供幫助,例如服務器端代碼。 – artbristol

+0

是的,在java中我做這樣的事情: protected void processRequest(HttpServletRequest req, HttpServletResponse resp) {...String name = req.getParameter("name");....}。我也嘗試過:使用URLDecoder.decode(名稱,「UTF-8」),但仍然一樣。這個頁面是用jquery動態加載的(這個頁面是一個彈出窗口),我如何強制這個帖子是UTF-8或頁面?謝謝。 –

回答

1

嘗試宣告HTML頁面中執行下列操作之一:在HTML頂部

<?xml version="1.0" encoding="UTF-8"?> 

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

OR

跨瀏覽器了兼容性,添加此page and

<head> 
    <meta http-equiv="Content-Type" content="application/xhtml+xml; charset=UTF-8"> 
... 
</head> 
+0

感謝您的回覆。你的解決方案促使我去搜索另一個:D,參見上面的更詳細的解釋。再次感謝你。 –