encodeURIComponent() 和 UTF-8 通过 AJAX 发送表单文本区域时


encodeURIComponent() and UTF-8 when sending Form textarea via AJAX

>我通过AJAX方法POST将数据发送到PHP后端文件。数据来自文本区域,其形式具有下一个属性accept-charset="UTF-8"。AJAX 函数以http[act].send("title=" + encodeURIComponent(field.value)...)形式发送数据,我的 PHP 后端文件以 header('Content-Type:text/html; charset=UTF-8'); 开头。数据库接受名称和字段都设置为 UTF-8(从来没有遇到过这个问题)。

我想我应该在 PHP 后端解码encodeURIComponent()函数。但是怎么做呢?

编辑(对不起):

Fibra dietética 作为 Fibra dietética
插入 DB 中Acompañada入为 Acompañada

。等等。

和平缔造者是对的在 Ajax 中,使用

encodeURIComponent()

然后在 PHP 中,使用

rawurldecode()

例(阿贾克斯)

abc=encodeURIComponent(xxx)

然后在 PHP 中,

$varName = rawurldecode($_POST['abc'])

使用rawurldecode($value)在这里看到 http://www.php.net/manual/en/function.rawurldecode.php

错误。在执行 INSERT 之前,我在字符串上使用htmlentities() (PHP)。一个不错的选择是使用strip_tags()(PHP)来避免"HTML/Js/...注射"。感谢大家的帮助。我希望这对某人有用。