jQuery 如何更改ajax字符集?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8285936/
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
How to change ajax-charset?
提问by user897237
How can I change the default encoding used by $.post()
?
如何更改 使用的默认编码$.post()
?
The arguments are encoded with UTF-8. How can I encode it with ISO 8859-1?
参数使用 UTF-8 编码。如何使用 ISO 8859-1 对其进行编码?
采纳答案by Sudhir Bastakoti
You could use:
你可以使用:
contentType:"application/x-javascript; charset:ISO-8859-1"
回答by gowtham kumar
By giving the content type explicitly during ajax call as below may allow you to override the default content type.
通过在 ajax 调用期间显式提供内容类型,如下所示可能允许您覆盖默认内容类型。
$.ajax({
data: parameters,
type: "POST",
url: ajax_url,
timeout: 20000,
contentType: "application/x-www-form-urlencoded;charset=ISO-8859-15",
dataType: 'json',
success: callback
});
You would also have to specify the charset on the server.
您还必须在服务器上指定字符集。
Ex: for php
例如:对于 php
<?php header('Content-Type: text/html; charset=ISO-8859-15'); ?>
I hope this may help you.
我希望这可以帮助你。
回答by Jan Molnar
It seems the charset cannot be changed anymore – $.ajax
docs states:
似乎字符集不能再更改了——$.ajax
文档说明:
The W3C XMLHttpRequest specification dictates that the charset is always UTF-8; specifying another charset will not force the browser to change the encoding.
W3C XMLHttpRequest 规范规定字符集始终为 UTF-8;指定另一个字符集不会强制浏览器更改编码。
回答by Roy Art
See section 4.5.6.4.4.3 of the XHR spec: https://xhr.spec.whatwg.org/#the-send()-method
请参阅 XHR 规范的第 4.5.6.4.4.3 节:https://xhr.spec.whatwg.org/#the-send()-method
If contentTypeRecord is not failure, contentTypeRecord's parameters["charset"] exists, and parameters["charset"] is not an ASCII case-insensitive match for "UTF-8", then:
Set contentTypeRecord's parameters["charset"] to "UTF-8".
如果 contentTypeRecord 没有失败,则 contentTypeRecord 的参数 ["charset"] 存在,并且参数 ["charset"] 不是“UTF-8”的 ASCII 不区分大小写匹配,则:
将 contentTypeRecord 的参数 ["charset"] 设置为 "UTF-8"。
The spec forces browsers to always send as UTF-8.
该规范强制浏览器始终以 UTF-8 格式发送。
You may however, use the fetch
API. Since it's not an XHR, posting using fetch will honour your encoding.
但是,您可以使用fetch
API。由于它不是 XHR,因此使用 fetch 发布将尊重您的编码。
fetch(url, {
method: 'POST',
headers: {
'Content-Type': `text/plain; charset=${yourCustomEncoding}`
},
body
}).then(...