How to provide variables represented in UTF8 with jquery $ .post

I struggled with this for three days, and this is what I have, and I don’t understand why I see this behavior.

My problem is that I have a MySql spanish db with setting and sorting char defined as utf8_general_ci. when I query the database in the delete.php file as follows: "DELETE FROM countryNames WHERE country = '$name'"

The specified string is not deleted. I set the variable $ name to delete.php through the variable post $name=$_post['data'] . basically $ name gets the value in Spanish characters like español, México etc. The delete.php file is called from main.php.if. I am sending a message from main.php $.post("delete.php", {data:filename}); , the request does not delete the record (although the string 'filename' is in utf8), but if I create a form and then send my data variable to main.php, the request works !! the big question for me is, why do I need to submit a request form? what i see is my database rejects the value if it comes from the jquery mailbox but accepts it when it is submitted from the submitted form. (I do not make code changes for the request to work. just post the value by submitting the form)

+6
source share
4 answers

Guys, this was a problem with the Mac! I just tested it on windows, like on my server, and now everything is working fine. Therefore, be careful when using a Mac as a server with MySql, having UTF8 as encoding and sorting. I assume that the Mac stores the folder and file name in a different encoding, and not UTF-8.

+1
source

First of all, to find out what encoding is used for requests, install something like Firebug and check the Content-Type header of your request / response. It will look something like this: application / json; encoding = ... '. This should be charset=utf-8 in your case.

My guess is why this worked when submitting the form, probably due to x-www-form-urlencoded - non-alphanumeric characters are additionally encoded on the client side and decoded again on the server, which is the difference in sending data directly.

This means that somewhere there is an incorrect encoding at work. PHP treats your strings agnostically by its default encoding, so I would prefer to exclude it as the source of the error. jQuery.post also uses UTF-8 by default ... so my suspect is the filename variable. Are you sure this is in UTF-8? Where and how do you extract it?

You should probably also make sure that the actual HTML page is also sent as UTF-8, and not, say, iso-8859-1. Take a look at the article for a detailed explanation of how to do this.

+2
source

The answer may be here: How to set encoding in .getJSON jQuery

As they say there, use $.ajax instead of $.post , and you can set the encoding.

OR, as said in the second answer, use $.ajaxSetup to set the encoding accordingly.

0
source

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

0
source

Source: https://habr.com/ru/post/895756/


All Articles