Jquery ajax post submits garbled data

  • 2020-03-26 23:00:41
  • OfStack

When dealing with html5 applications with jquery, all tests under firefox were normal. Users said there were some garbled codes when accessing them with pad.
Sure enough, I found that both chrome and ie kernels had this problem. When the page property was set to utf-8, only firefox passed charset=utf-8 header file
Neither chrome nor ie is specified, so there is a garbled problem.

  contentType: "application/x-www-form-urlencoded; charset=utf-8"
$.post("test.php", { name: "i5a6", time: "2pm" },
   }, "json");

Or use:

  contentType:"application/x-www-form-urlencoded; charset=utf-8",
  success: function(){

Recommend to use the first, but also according to their own actual situation to see, some people recommend to use encodeURIComponent character conversion
Summarize a few experiences with ajax submission data garbled code
To avoid garbled code, you can do the following steps
The solution
1. Maintain uniform encoding, including file encoding, database encoding and webpage content-type encoding
Check it out < Meta content-type HTTP - equiv = "" content =" text/HTML. Charset = utf-8 "/ >
It is recommended to use utf-8 in Chinese, and GBK /gb2312 may lead to garble codes
2, use post to send instead of get
The get method passes the parameters through a link and automatically urlEncode, which may vary from browser to browser. You can avoid this by using post.
3. The escape code in the front of js is sent, and then the data is acquired by decoding in the background
These can be searched on the Internet
4. Set the global contentType and specify the encoding
Since jquery ajax USES utf-8 to encode sending data, ie does not add charset=utf-8 when sending, resulting in garble (ie USES iso-8859-1 encoding by default)

 contentType: "application/x-www-form-urlencoded; charset=utf-8"

Related articles: