Javascript JQuery AJAX 不向我的服务器发送 UTF-8,仅在 IE 中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2473316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:31:09  来源:igfitidea点击:

JQuery AJAX is not sending UTF-8 to my server, only in IE

javascriptjqueryajaxutf-8internet-explorer-8

提问by TIMEX

I am sending UTF-8, japanese text, to my server. It works in Firefox. My access.log and headers are:

我正在向我的服务器发送 UTF-8(日语文本)。它适用于 Firefox。我的 access.log 和标题是:

/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:

Howeer,在 IE8 中,我的 access.log 说:

/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 ...,
    ...
    })

回答by Gabriele Petrioli

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处理编码..

回答by javabeangrinder

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 中的两个字符扩展字符,就好像它们产生一个字符一样在服务器发送一个字符太少。

See what I did to solve it here: Not well formed Json when sending to CouchDB

看看我在这里做了什么来解决它:发送到 CouchDB 时格式不正确的 Json

回答by Alfonso

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,但我确信每种服务器端语言都有一个选项。这只是几件事:

  1. 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.

  2. 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.

  1. 通过添加 header('Content-Type: text/html; charset=utf-8'); 确保您在 ajax 响应中发送正确的标题。这必须是您的第一行。如果您有任何错误说标头已经发送或类似的错误是因为在您的代码中的某处您在发送标头之前输出了额外的空格或其他内容,因此请检查您的代码。

  2. 在服务器中构建响应时,请确保使用 echo htmlentities($your-string, null, 'utf-8); 将所有字符转换为相应的 HTML 字符。因为即使在告诉 IE 您正在发送 utf-8 数据之后,IE 似乎也忘记了这一点,或者它不会简单地假设任何事情,因此将其添加到您的代码中将确保正确的输出。

Thanks all for your help.

感谢你的帮助。

回答by ali.b.y

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();
};