另一个 JQuery 编码问题,在 IE 上
我正在编写一个意大利网站,我需要通过 xhr 调用验证一些输入数据。 我的 ajax 请求代码是这样的(我使用的是 JQuery 1.3.2):
$.ajaxSetup({
type: "POST",
timeout: 10000,
contentType: "application/x-www-form-urlencoded; charset=iso-8859-1"
});
$.ajax({
url: "ajaxvalidate.do",
data: {field:controlInfo.field,value:controlInfo.fieldValue},
dataType: "json",
complete: function() {
//
},
success: function(msg) {
handleAsyncMsg(controlInfo, msg, closureOnError);
},
error: function(xhr, status, e) {
showException(controlInfo.id, status);
}
});
在后端,我有一个 java struts 操作来处理 xhr。 我需要在页面中使用编码 ISO-8859-1 以确保数据(特殊重音字符)在同步提交中正确发送。
一切都在 Firefox 中运行得很好,但是当我必须处理来自 IE 7 的带重音字符的异步帖子时,我遇到了一个问题:我总是收到无效字符(也许是 utf-8?)。 EG 我以 àààààààà 的形式输入,然后在请求中得到这个值:à à à à à à。 由于请求字符集已正确设置为 ISO-8859-1,我无法理解为什么服务器仍然无法正确解析表单值。
这是一个包含所有请求标头和错误的日志示例(服务器是旧的 Bea Weblogic 8.1):
Encoding: ISO-8859-1
Header: x-requested-with - Value: XMLHttpRequest
Header: Accept-Language - Value: it
Header: Referer - Value: https://10.172.14.36:7002/reg-docroot/conv/starttim.do
Header: Accept - Value: application/json, text/javascript
Header: Content-Type - Value: application/x-www-form-urlencoded; charset=iso-8859-1
Header: UA-CPU - Value: x86
Header: Accept-Encoding - Value: gzip, deflate
Header: User-Agent - Value: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
Header: Host - Value: 10.172.14.36:7002
Header: Content-Length - Value: 65
Header: Connection - Value: Keep-Alive
Header: Cache-Control - Value: no-cache
Header: Cookie - Value: JSESSIONID=JQJlNpVC86yTZJbcpt54wzt82TnkYmWYC5VLL2snt5Z8GTsQ1pLQ!1967684811
Attribute: javax.net.ssl.cipher_suite - Value: SSL_RSA_WITH_RC4_128_MD5
Attribute: javax.servlet.request.key-size - Value: 128
Attribute: javax.servlet.request.cipher_suite - Value: TLS_RSA_WITH_RC4_128_MD5
Attribute: javax.servlet.request.key_size - Value: 128
Attribute: weblogic.servlet.network_channel.port - Value: 7001
Attribute: weblogic.servlet.network_channel.sslport - Value: 7002
Attribute: org.apache.struts.action.MESSAGE - Value: org.apache.struts.util.PropertyMessageResources@4a97dbd
Attribute: org.apache.struts.globals.ORIGINAL_URI_KEY - Value: /conv/ajaxvalidate.do
Attribute: errors - Value: org.apache.struts.util.PropertyMessageResources@4a97e4d
Attribute: org.apache.struts.action.MODULE - Value: org.apache.struts.config.impl.ModuleConfigImpl@4aa2ff8
Attribute: weblogic.servlet.request.sslsession - Value: javax.net.ssl.impl.SSLSessionImpl@42157c5
field: nome - value: Ã Ã Ã Ã Ã Ã Ã Ã - action: /endtim
I'm coding an italian website where I need to validate some input data with an xhr call.
My code for the ajax request's like this (I'm using JQuery 1.3.2):
$.ajaxSetup({
type: "POST",
timeout: 10000,
contentType: "application/x-www-form-urlencoded; charset=iso-8859-1"
});
$.ajax({
url: "ajaxvalidate.do",
data: {field:controlInfo.field,value:controlInfo.fieldValue},
dataType: "json",
complete: function() {
//
},
success: function(msg) {
handleAsyncMsg(controlInfo, msg, closureOnError);
},
error: function(xhr, status, e) {
showException(controlInfo.id, status);
}
});
On the backend I have a java struts action to handle the xhr. I need to use the encoding ISO-8859-1 in the page to ensure the data (specially accented characters) are sent correctly in the synchronous submit.
All's working like a charm in Firefox but when I have to handle an async post from IE 7 with accented characters I have a problem: I always receive invalid characters (utf-8 maybe?).
EG I type in the form àààààààà and I get in my request this value: Ã Ã Ã Ã Ã Ã Ã Ã.
Since the request charset's correctly set to ISO-8859-1 I can't understand why the server's still not parsing the form value correctly.
This is a log sample with all the request headers and the error (the server's an old Bea Weblogic 8.1):
Encoding: ISO-8859-1
Header: x-requested-with - Value: XMLHttpRequest
Header: Accept-Language - Value: it
Header: Referer - Value: https://10.172.14.36:7002/reg-docroot/conv/starttim.do
Header: Accept - Value: application/json, text/javascript
Header: Content-Type - Value: application/x-www-form-urlencoded; charset=iso-8859-1
Header: UA-CPU - Value: x86
Header: Accept-Encoding - Value: gzip, deflate
Header: User-Agent - Value: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
Header: Host - Value: 10.172.14.36:7002
Header: Content-Length - Value: 65
Header: Connection - Value: Keep-Alive
Header: Cache-Control - Value: no-cache
Header: Cookie - Value: JSESSIONID=JQJlNpVC86yTZJbcpt54wzt82TnkYmWYC5VLL2snt5Z8GTsQ1pLQ!1967684811
Attribute: javax.net.ssl.cipher_suite - Value: SSL_RSA_WITH_RC4_128_MD5
Attribute: javax.servlet.request.key-size - Value: 128
Attribute: javax.servlet.request.cipher_suite - Value: TLS_RSA_WITH_RC4_128_MD5
Attribute: javax.servlet.request.key_size - Value: 128
Attribute: weblogic.servlet.network_channel.port - Value: 7001
Attribute: weblogic.servlet.network_channel.sslport - Value: 7002
Attribute: org.apache.struts.action.MESSAGE - Value: org.apache.struts.util.PropertyMessageResources@4a97dbd
Attribute: org.apache.struts.globals.ORIGINAL_URI_KEY - Value: /conv/ajaxvalidate.do
Attribute: errors - Value: org.apache.struts.util.PropertyMessageResources@4a97e4d
Attribute: org.apache.struts.action.MODULE - Value: org.apache.struts.config.impl.ModuleConfigImpl@4aa2ff8
Attribute: weblogic.servlet.request.sslsession - Value: javax.net.ssl.impl.SSLSessionImpl@42157c5
field: nome - value: Ã Ã Ã Ã Ã Ã Ã Ã - action: /endtim
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以说您在标头中以 ISO-8859-1 形式发送表单提交,但那并不意味着你实际上是。 jQuery 使用标准 JavaScriptencodeURIComponent() 方法将 Unicode 字符串编码为查询字符串字节,并且始终使用 UTF-8。
无论如何,MIME 类型“application/x-www-form-urlencoded”的“charset”参数是高度非标准的。 作为“x-”类型,该类型没有官方 MIME 注册,但 HTML 4.01 没有指定这样的参数,对于“application/*”类型来说这是非常不寻常的。 Weblogic 声称能够检测到这种构造,无论其价值如何。
因此,您可以做的是:
1:自己创建 POST 正文表单 urlencoded 内容,手动将其破解为 ISO-8859-1 格式,使用类似的方法
而不是encodeURIComponent()。
2:丢失“字符集”并使其正常提交 UTF-8,并使您的 servlet 理解传入的 UTF-8。 这通常是最好的,但意味着要修改 servlet 容器配置以使其选择正确的编码。 对于 Weblogic,这似乎意味着 使用weblogic.xml 中的元素 。 到那时,您正在考虑将整个应用程序迁移到 UTF-8。 这绝不是一件坏事(不支持 Unicode 的网站太 20 世纪了!),但可能需要大量工作。
You can say you're sending a form submission as ISO-8859-1 in the header, but that doesn't mean you actually are. jQuery uses the standand JavaScript encodeURIComponent() method to encode Unicode strings into query-string bytes, and that always uses UTF-8.
In any case, the ‘charset’ parameter for the MIME type ‘application/x-www-form-urlencoded’ is highly non-standard. As an ‘x-’ type there is no official MIME registration for this type, but HTML 4.01 doesn't specify such a parameter and it would be very unusual for an ‘application/*’ type. Weblogic claims to detect this construct, for what it's worth.
So what you can do is either:
1: create the POST body form-urlencoded content yourself, hacking it into ISO-8859-1 format manually, using something like
instead of encodeURIComponent().
2: lose the ‘charset’ and leave it submitting UTF-8 as normal, and make your servlet understand incoming UTF-8. This is generally best, but will mean mucking around with the servlet container config to make it choose the right encoding. For Weblogic this seems to mean using an <input-charset> element in weblogic.xml. And by then you're looking at moving your whole app to UTF-8. Which is by no means a bad thing (non-Unicode-capable websites are sooo 20-century!) but may well be a lot of work.
实际上只是解决了这个问题,我需要这样做:
jQuery.ajaxSetup({
内容类型:“application/json;charset=utf-8”
});
这会覆盖将 application/x-www-form-urlencoded 添加到 IE 中的内容类型的行为。
Actually just solved that, I needed to do:
jQuery.ajaxSetup({
contentType: "application/json;charset=utf-8"
});
Which overrides the behaviour of adding application/x-www-form-urlencoded to the content-type in IE.
我在西班牙门户网站的内容评论系统中工作时遇到了类似的问题。
经过几个小时的搜索,最终解决了我的问题,而不是弄乱 jQuery 字符集,无论如何,它似乎都使用 utf-8,而是在处理的 PHP 中从 utf-8 解码回 ISO-8859-1 ajax 发布。 在 PHP 中,有一个内置函数 utf8_decode(),所以我对注释字符串所做的第一件事是:
$comentario = utf8_decode($_POST['comentario']);
(然后我使用 nl2br() 和 htmlentities() PHP 函数来准备要使用 html 实体而不是特殊字符存储的文本)
Good Luck & 一切平安!
塞巴
I was having similar problems, working in a content comments system in our Spanish Portal.
What finally solved my problem, after many hours of searching, instead of messing with jQuery charset, that seems to use utf-8 no matter what, it was to decode from utf-8 back to ISO-8859-1 in the PHP that processed the ajax POST. In PHP there is a built in function, utf8_decode(), so the first thing I do with the comments string is this:
$comentario = utf8_decode($_POST['comentario']);
(and then I used nl2br() and htmlentities() PHP functions in order to prepare the text to be stored with html entities instead of special chars)
Good Luck & Peace all over!
Seba