I am sending UTF-8, japanese text, to my server. It works in Firefox. My access.log and headers are:
我发送utf - 8,日本文字,我的服务器。它工作在Firefox。我的访问。日志和标题是:
/ajax/?q=%E6%BC%A2%E5%AD%97
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Content-Type application/x-www-form-urlencoded; charset=UTF-8
Howeer, in IE8, my access.log says:
然而,在IE8中,我的访问权限。日志说:
/ajax/?q=??
For some reason, IE8 is turning my AJAX call into question marks. Why!? I added the scriptCharset and ContentType according to some tutorials, but still no luck.
出于某种原因,IE8正在把我的AJAX调用变成问号。为什么! ?根据一些教程,我添加了scriptCharset和ContentType,但仍然没有成功。
And this is my code:
这是我的代码:
$.ajax({
method:"get",
url:"/ajax/",
scriptCharset: "utf-8" ,
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
data:"q="+query ...,
...
})
4 个解决方案
#1
54
Try encoding the query parameter with encodeURIComponent()
尝试使用encodeURIComponent()对查询参数进行编码
data:"q="+encodeURIComponent( query )
as bobince very correctly noted in his comment, if you use the object notation to pass parameters to the ajax method it will handle the encoding itself..
正如bobince在他的注释中非常正确地指出的,如果您使用对象表示法将参数传递给ajax方法,它将处理编码本身。
so
所以
data:{ q : query }
will make jQuery handle the encoding ..
将使jQuery处理编码。
#2
2
I'we read this post hoping it would solve the problem I had came across and that had to do with utf8 conversions.
我读了这篇文章,希望它能解决我遇到的问题,这与utf8转换有关。
In my case it turned out that the server engine (node.js) calculating the Content-length of the data with the data considered to be raw and not utf8, thus two character extended chars in uft8 was calculated as if they where one char resulting in the server sending one character too little.
在我的例子中原来服务器引擎(node . js)内容长度计算的数据与数据被认为是原始而不是utf8,因此两个字符扩展字符在计算uft8好像一个char导致服务器发送一个字符太少。
See what I did to solve it here: Not well formed Json when sending to CouchDB
看看我在这里做了什么:发送到CouchDB时,Json格式不太好
#3
0
I know this is an old post but I had this problem recently and I'd like to contribute just in case someone else has the same problem. I'm using PHP but I'm sure there's an option on every serverside language. It was just a couple of things:
我知道这是一个老帖子,但我最近有这个问题,我想贡献,以防别人也有同样的问题。我正在使用PHP,但我确信在每一种服务器端语言中都有一个选项。只有几件事:
-
Make sure you're sending the right headers on your ajax response by adding header('Content-Type: text/html; charset=utf-8'); This must be your first line. If you have any errors saying that headers have been sent already or something like that is because somewhere in your code you are outputing an extra space or something before sending the header so check your code.
通过添加header('Content-Type: text/html;charset = utf - 8 ');这一定是你的第一行。如果你有任何错误说标题已经被发送或者类似的东西是因为在你的代码中你在发送标题之前输出了一个额外的空间或者其他东西,所以检查你的代码。
-
When you build your response in your server, make sure you convert all your chars to the correspondig HTML char using echo htmlentities($your-string, null, 'utf-8); Because even after telling IE that you are sending utf-8 data, it seems like IE forgets that or it doesn't simply assume anything so adding this to your code will ensure the right output.
当您在服务器中构建响应时,请确保使用echo htmlentities($your-string, null, 'utf-8)将所有的chars转换为相应的HTML字符;因为即使告诉IE你正在发送utf-8数据,IE似乎也会忘记这一点,或者它不会简单地假设任何事情,所以在代码中添加这些数据将确保正确的输出。
Thanks all for your help.
谢谢你的帮助。
#4
0
Use encodeURIComponent()
in javaScript. Here is the sample:
使用encodeURIComponent()的javaScript。这是示例:
function doPost()
{
var URL = "http://localhost/check.php?yab=" + encodeURIComponent(document.getElementById("formSearch").childNodes[1].value);
xmlHttp.open("GET", URL);
xmlHttp.send();
};
#1
54
Try encoding the query parameter with encodeURIComponent()
尝试使用encodeURIComponent()对查询参数进行编码
data:"q="+encodeURIComponent( query )
as bobince very correctly noted in his comment, if you use the object notation to pass parameters to the ajax method it will handle the encoding itself..
正如bobince在他的注释中非常正确地指出的,如果您使用对象表示法将参数传递给ajax方法,它将处理编码本身。
so
所以
data:{ q : query }
will make jQuery handle the encoding ..
将使jQuery处理编码。
#2
2
I'we read this post hoping it would solve the problem I had came across and that had to do with utf8 conversions.
我读了这篇文章,希望它能解决我遇到的问题,这与utf8转换有关。
In my case it turned out that the server engine (node.js) calculating the Content-length of the data with the data considered to be raw and not utf8, thus two character extended chars in uft8 was calculated as if they where one char resulting in the server sending one character too little.
在我的例子中原来服务器引擎(node . js)内容长度计算的数据与数据被认为是原始而不是utf8,因此两个字符扩展字符在计算uft8好像一个char导致服务器发送一个字符太少。
See what I did to solve it here: Not well formed Json when sending to CouchDB
看看我在这里做了什么:发送到CouchDB时,Json格式不太好
#3
0
I know this is an old post but I had this problem recently and I'd like to contribute just in case someone else has the same problem. I'm using PHP but I'm sure there's an option on every serverside language. It was just a couple of things:
我知道这是一个老帖子,但我最近有这个问题,我想贡献,以防别人也有同样的问题。我正在使用PHP,但我确信在每一种服务器端语言中都有一个选项。只有几件事:
-
Make sure you're sending the right headers on your ajax response by adding header('Content-Type: text/html; charset=utf-8'); This must be your first line. If you have any errors saying that headers have been sent already or something like that is because somewhere in your code you are outputing an extra space or something before sending the header so check your code.
通过添加header('Content-Type: text/html;charset = utf - 8 ');这一定是你的第一行。如果你有任何错误说标题已经被发送或者类似的东西是因为在你的代码中你在发送标题之前输出了一个额外的空间或者其他东西,所以检查你的代码。
-
When you build your response in your server, make sure you convert all your chars to the correspondig HTML char using echo htmlentities($your-string, null, 'utf-8); Because even after telling IE that you are sending utf-8 data, it seems like IE forgets that or it doesn't simply assume anything so adding this to your code will ensure the right output.
当您在服务器中构建响应时,请确保使用echo htmlentities($your-string, null, 'utf-8)将所有的chars转换为相应的HTML字符;因为即使告诉IE你正在发送utf-8数据,IE似乎也会忘记这一点,或者它不会简单地假设任何事情,所以在代码中添加这些数据将确保正确的输出。
Thanks all for your help.
谢谢你的帮助。
#4
0
Use encodeURIComponent()
in javaScript. Here is the sample:
使用encodeURIComponent()的javaScript。这是示例:
function doPost()
{
var URL = "http://localhost/check.php?yab=" + encodeURIComponent(document.getElementById("formSearch").childNodes[1].value);
xmlHttp.open("GET", URL);
xmlHttp.send();
};