Ajax 中的编码是什么?

发布于 2024-08-10 22:22:39 字数 81 浏览 1 评论 0原文

通常我们使用 UTF-8 编码标准来发送每种语言的请求。 但在某些语言中,这种编码标准无法正常工作,那么在这种情况下我们使用 ISO-8859-1。

Generally we are using UTF-8 encoding standard for sending the request for every language.
But in some language this encoding standard is not working properly,then in that case we are using
ISO-8859-1.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情泪▽动烟 2024-08-17 22:22:39

您可以使用任何您想要的编码。然而,从你的问题来看,听起来你通常使用 UTF-8,但有时你会从使用不同编码的地方获取数据(例如,Internet Explorer 倾向于使用 ISO- 将数据发送到 Web 服务器) 8859-1)。

如果您要提供 UTF-8 编码的文本,并且从某处获取非 UTF-8 编码的文本,则必须在发送之前将其转换为 UTF-8。一个好的做法可能是自动清理从 Web 浏览器接收到的所有数据并将其重新编码为 UTF-8。不幸的是,浏览器并不总是告诉您它正在使用什么编码;如果未提供,您可能会假设它是 UTF-8 或 ISO-8859-1。

如果您使用服务器端语言,您将需要研究如何使用该语言转换编码。例如,PHP 有 iconv() 函数调用,以及一个非常好的函数 mb_detect_encoding($text),它可以很好地猜测某个文件的编码。当你还不知道时给定一些数据。

类似这样的事情将是有序的(假设 PHP 服务器端):

$text = iconv(mb_detect_encoding($text), 'UTF-8', $text);

在对所有用户输入执行任何其他操作之前,先对所有用户输入执行此操作(例如,使用 array_map 自动转换用户输入):

function convert_to_utf8($text) {
    return iconv(mb_detect_encoding($text), 'UTF-8', $text);
}
$_GET = array_map('convert_to_utf8', $_GET);
$_POST = array_map('convert_to_utf8', $_POST);

最好的方法是确定浏览器是否提供编码,并将其用作 iconv() 的第一个参数,而不是 mb_detect_encoding。

You can use any encoding you want. However from your question, it sounds like typically you're using UTF-8, but sometimes you're getting data from somewhere that's coming in with a different encoding (eg, Internet Explorer tends to like send data to the web server using ISO-8859-1).

If you're going to serve up UTF-8 encoded text, and you get non-UTF-8 encoded text from somewhere, you have to convert that to UTF-8 before you send it down the line. Probably a good practice is to automatically sanitize all data received from the web browser and re-encode it as UTF-8. Unfortunately the browser doesn't always tell you what encoding it's using; if it's not supplied you can probably assume it's UTF-8 or ISO-8859-1.

If you're using a server side language, you're going to want to look into how to convert encodings with that language. For example, PHP has iconv() function calls, and a very nice function mb_detect_encoding($text) which will do a pretty decent job of guessing what the encoding is for a given bit of data when you don't already know.

Something like this would be in order (presuming PHP serverside):

$text = iconv(mb_detect_encoding($text), 'UTF-8', $text);

Do this with all user input before you do anything else with it (eg, use array_map to automatically convert user inputs):

function convert_to_utf8($text) {
    return iconv(mb_detect_encoding($text), 'UTF-8', $text);
}
$_GET = array_map('convert_to_utf8', $_GET);
$_POST = array_map('convert_to_utf8', $_POST);

Best yet would be to determine if the browser is supplying an encoding, and use that as the first argument to iconv() instead of mb_detect_encoding.

情泪▽动烟 2024-08-17 22:22:39

这是一个相当模糊的问题。

如果您想问“AJAX 中的编码是什么?”那么答案是AJAX不是一种编码,它是一种客户端-服务器通信的方法。

如果您想问“AJAX 使用什么编码?”那么答案是 AJAX 响应可以使用您想要的任何编码,但它通常应该与发出请求的 HTML 页面的编码相匹配。

This is a rather vague question.

If you mean to ask, "what is encoding in AJAX?" then the answer is that AJAX is not an encoding, it is a method of client-server communication.

If you meant to ask, "what encoding does AJAX use?" then the answer is that AJAX responses can use whatever encoding you want, but it should typically match the encoding of the HTML page that made the request.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文