如何使用jquery $.post确保变量以UTF8提交
我已经为此苦苦挣扎了三天,这就是我所得到的,我不明白为什么我会看到这种行为。
我的问题是我有一个 MySql 西班牙语数据库,其字符集和排序规则定义为 utf8_general_ci。当我像这样“DELETE FROM CountryNames WHERE Country = '$name'”
查询delete.php中的数据库时,
指定的行不会被删除。我通过 post 变量 $name=$_post['data']
在 delete.php 中设置变量 $name 。大多数 $name 获取西班牙语字符的值,例如 español、México 等。delete.php 文件从 main.php 调用。如果我从 main.php 发送帖子消息 $.post("delete.php", {data:filename});
,查询不会删除条目(尽管“文件名”字符串采用 utf8 格式),但如果我创建一个表单,然后在 main.php 中发布我的数据变量,则查询有效!对我来说最大的问题是为什么我必须提交表格才能进行查询?我看到的是,如果该值来自 jquery post 调用,则我的数据库会拒绝该值,但如果该值来自提交的表单,则数据库会接受该值。 (我没有对查询进行任何代码更改。只需通过提交表单来发布值)
I have been struggling with this for three days now and this is what i have got and i cannot understand why i am seeing this behavior.
my problem is that i have a MySql spanish db with char set and collation defined as utf8_general_ci. when i query the data base in delete.php like this "DELETE FROM countryNames WHERE country = '$name'"
the specified row doesnot get deleted. i am setting the variable $name in delete.php through a post variable $name=$_post['data']
. mostly $name gets the value in spanish characters e.g español, México etc. the delete.php file gets called from main.php.if i send a post message from main.php $.post("delete.php", {data:filename});
, the query doesnot deletes the entry (although the 'filename' string is in utf8) but if i create a form and then post my data variable in main.php, the query works!! the big question to me is why do i have to submit a form for the query to work? what im seeing is my database rejects the value if it comes from a jquery post call but accepts it when its from a submitted form. (i make no code change for the query to work. just post the value by submiting the form)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,要查看请求使用的字符集,请安装 Firebug 之类的东西并检查请求/响应的“Content-Type”标头。它看起来像“application/json;”字符集=...'。在您的情况下,这应该是
charset=utf-8
。我猜为什么它在发布表单时起作用可能是因为 x-www-form-urlencoded - 非字母数字字符在客户端额外编码并在服务器上再次解码,这是与直接发布数据的区别。
这意味着某个地方存在错误的编码。默认情况下,PHP 对待字符串与其编码无关,因此我倾向于将其排除为错误来源。
jQuery.post
默认情况下也使用 UTF-8...所以我怀疑是filename
变量。你确定它是UTF-8格式的吗?您在哪里以及如何检索它?您可能还应该确保实际的 HTML 页面也以 UTF-8 形式发送,而不是以 iso-8859-1 形式发送。请查看这篇文章,了解如何正确使用的详细说明。
First of all, to see what charset ìs used for requests, install something like Firebug and check the 'Content-Type' header of your request/response. It will look something like 'application/json; charset=...'. This should be
charset=utf-8
in your case.My guess why it worked when posting a form is probably because of x-www-form-urlencoded - non-alphanumeric characters are additionally encoded on the client side and again decoded on the server, that's the difference to posting the data directly.
This means that somewhere there is a wrong encoding at work. PHP treats your strings agnostic to its encoding by default, so I would tend to rule it out as the source of the error.
jQuery.post
also uses UTF-8 by default... so my suspect is thefilename
variable. Are you sure it is in UTF-8? Where and how do you retrieve it?You should probably also ensure that the actual HTML page is also sent as UTF-8 and not, let's say iso-8859-1. Have a look at this article for a thorough explanation on how to get it right.
伙计们,这是 Mac 的问题!我刚刚在 Windows 上测试了它作为我的服务器,现在一切正常。因此,当您使用 Mac 作为服务器并使用 UTF8 作为字符集和排序规则的 MySql 时要小心。我猜 Mac 以某种不同的编码而不是 UTF-8 存储文件夹和文件名。
guys this was a Mac problem!! i just tested it on windows as my server and now everything works fine. So beware when u r using Mac as a server with MySql having UTF8 as charset and collation. I guess the Mac stores the folder and file name in some different encoding and not UTF-8.
您的答案可能在这里:如何在 .getJSON JQuery 中设置编码
正如它所说,使用
$.ajax
而不是$.post
并且您可以设置编码。或者,正如第二个答案中所述,使用
$.ajaxSetup
相应地设置编码。You answer might be here: How to set encoding in .getJSON JQuery
As it says there, use
$.ajax
instead of$.post
and you can set encoding.OR, as it says in the 2nd answer use
$.ajaxSetup
to set the encoding accordingly.使用 .serialize() !我认为它会起作用。更多信息:http://api.jquery.com/serialize/
Use .serialize() ! I think it will work. More info: http://api.jquery.com/serialize/