jQuery:Ajax发布和编码 [英] Jquery: ajax post and encoding
问题描述
我无法理解为什么我无法从服务器答案中获得正确的ISO-8859-1字符集.将其作为遗留代码的作品,我几乎无法更改页面上的字符集编码.
I am unable to understand why I can't get a correct ISO-8859-1 charstet from the server answer. Being this a work on legacy code, i hardly could change charset encoding on the pages.
我利用了JQuery调用
I make use of the JQuery call
$.post("server-side-code", {t:ctext, i:ioff, sid:sessionid},
function(data, status) {
$('#chk').append(data);
});
发布使用javascript创建的textarea值:
posting a textarea value created using javascript:
<form accept-charset='ISO-8859-1' method='post'>
<textarea cols='40' rows='8' id='commento'></textarea><br>
<input type='button' value='invia' id='submit'></form>
处理请求的服务器端脚本在其顶部声明:
The server side script processing the request declares at its very top:
text/html; charset=ISO-8859-1
因此,说实话,就编码而言,我不知道应该声明什么.尽管如此,当将服务器答案放在HTML元素中时,带重音的字符àèéìòù"会弹回为ÃÃéì¬Ã²Ã¹"
so, honestly, I can't figure out what else I should declare, in terms of encoding. This notwithstanding, the accented characters "àèéìòù" bounce back as: "à èéìòù" when placing the server answer in an HTML element
源被另存为ascii.尝试执行此操作以使要发布的变量具有基本的HTML编码无法解决:
The source is saved as ascii. Tryng to do this to have rudimentary Html encoding on the variable to be posted does not solve:
ctext = escapeHTML(ctext);
function escapeHTML (str)
{
var div = document.createElement('div');
var text = document.createTextNode(str);
div.appendChild(text);
return div.innerHTML;
};
有什么主意吗?
谢谢!
推荐答案
我现在有一个更好的解决方案.都可以正确发布并获得作品. 我正在处理默认情况下处理ISO 8859内容的tomcat.
I have a better solution now. Both post and get works PERFECTLY. I'm working over tomcat who by default handle ISO 8859 stuff.
网页属性:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
头部内部网页的字符集.
charset of the webpage inside the head.
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
现在,我所有的参数都通过转义功能转义了,我之前提供了此解决方案.
Now, all my parameteres are escaped with escape function, I provided this solution before.
(function($) {$.fn.escape = function() {
return escape(this.val());};})(jQuery);
现在,在发送ajax请求时,我将contentType设置为此:
Now, when sending the ajax request I set the contentType to this:
contentType : "application/x-www-form-urlencoded; charset=iso-8859-1"
最后,当在servlet或任何接收器处接收参数时,我使用此参数获得了解码后的参数.
And finally , when receiving the parameters at the servlet or any receiver I get a decoded parameter using this.
public String getHttpParameter(String name) throws Exception {
String value = this.getHttpRequest().getParameter(name);
return value == null || value.isEmpty() ? value : URLDecoder.decode(value,"ISO-8859-1");
}
POST和GET在IE 7和8,SAFARI,CHROME和FIREFOX中完美运行.
POST and GET works perfectly in IE 7 and 8 , SAFARI, CHROME and FIREFOX.
这篇关于jQuery:Ajax发布和编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!