如何确保使用jquery $.post在UTF8中提交的变量

时间:2022-09-26 08:55:50

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'"

我的问题是,我有一个带有char设置和排序的MySql西班牙db,定义为utf8_general_ci。当我在delete中查询数据库时。php类似于"从国家名中删除,其中国家= '$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)

指定的行不会被删除。我在delete中设置变量$name。php通过post变量$name=$_post['data']。$name通常用西班牙语字符e表示。西班牙,墨西哥等国家删除。从main.php调用php文件。如果我从main发送邮件。php $ . post(“删除。php”,{数据:文件名});,查询不会删除条目(尽管“文件名”字符串在utf8中),但是如果我创建一个表单,然后将数据变量以main的形式发布的话。php,查询工作! !对我来说,最大的问题是为什么我必须提交一个表单才能进行查询?我看到的是我的数据库拒绝来自jquery post调用的值,但接受来自提交表单的值。(对于查询,我不做任何代码更改。只要提交表单就可以了)

4 个解决方案

#1


2  

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.

首先,要查看请求使用的字符集,请安装Firebug之类的东西,并检查请求/响应的“Content-Type”头。它看起来像'application/json;charset =…”。在您的例子中,应该是charset=utf-8。

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.

我猜测,在发布表单时,它为什么会起作用,可能是因为x-www-form- urlencodes—非字母数字字符在客户端被额外编码,然后在服务器上再次解码,这与直接发布数据是不同的。

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 the filename variable. Are you sure it is in UTF-8? Where and how do you retrieve it?

这意味着在某些地方存在错误的编码。PHP默认情况下不考虑字符串的编码,因此我倾向于将其排除为错误的来源。jQuery。post默认也使用UTF-8…我的怀疑对象是文件名变量。你确定是UTF-8吗?你在哪里以及如何取回它?

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.

您还应该确保实际的HTML页面也被发送为UTF-8,而不是,比方说iso-8859-1。看一看这篇文章,就如何正确地做一个详细的解释。

#2


1  

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.

伙计们,这是Mac的问题!!我刚刚在windows上测试了它作为我的服务器,现在一切正常。所以,当你使用Mac作为一个服务器时,要小心,因为MySql的UTF8是charset和collation。我猜Mac将文件夹和文件名存储在不同的编码中,而不是UTF-8。

#3


0  

You answer might be here: How to set encoding in .getJSON JQuery

您的答案可能是:如何在.getJSON JQuery中设置编码

As it says there, use $.ajax instead of $.post and you can set encoding.

正如上面所说,使用$。ajax而不是美元。post和您可以设置编码。

OR, as it says in the 2nd answer use $.ajaxSetup to set the encoding accordingly.

或者,就像第二种回答说的,用$。ajaxSetup相应地设置编码。

#4


0  

Use .serialize() ! I think it will work. More info: http://api.jquery.com/serialize/

使用.serialize()!我想这行得通。更多信息:http://api.jquery.com/serialize/。

#1


2  

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.

首先,要查看请求使用的字符集,请安装Firebug之类的东西,并检查请求/响应的“Content-Type”头。它看起来像'application/json;charset =…”。在您的例子中,应该是charset=utf-8。

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.

我猜测,在发布表单时,它为什么会起作用,可能是因为x-www-form- urlencodes—非字母数字字符在客户端被额外编码,然后在服务器上再次解码,这与直接发布数据是不同的。

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 the filename variable. Are you sure it is in UTF-8? Where and how do you retrieve it?

这意味着在某些地方存在错误的编码。PHP默认情况下不考虑字符串的编码,因此我倾向于将其排除为错误的来源。jQuery。post默认也使用UTF-8…我的怀疑对象是文件名变量。你确定是UTF-8吗?你在哪里以及如何取回它?

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.

您还应该确保实际的HTML页面也被发送为UTF-8,而不是,比方说iso-8859-1。看一看这篇文章,就如何正确地做一个详细的解释。

#2


1  

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.

伙计们,这是Mac的问题!!我刚刚在windows上测试了它作为我的服务器,现在一切正常。所以,当你使用Mac作为一个服务器时,要小心,因为MySql的UTF8是charset和collation。我猜Mac将文件夹和文件名存储在不同的编码中,而不是UTF-8。

#3


0  

You answer might be here: How to set encoding in .getJSON JQuery

您的答案可能是:如何在.getJSON JQuery中设置编码

As it says there, use $.ajax instead of $.post and you can set encoding.

正如上面所说,使用$。ajax而不是美元。post和您可以设置编码。

OR, as it says in the 2nd answer use $.ajaxSetup to set the encoding accordingly.

或者,就像第二种回答说的,用$。ajaxSetup相应地设置编码。

#4


0  

Use .serialize() ! I think it will work. More info: http://api.jquery.com/serialize/

使用.serialize()!我想这行得通。更多信息:http://api.jquery.com/serialize/。