Home >Web Front-end >JS Tutorial >How to solve jquery $.post garbled problem
Jquery $.post garbled solution: 1. Modify the code to "contentType: "application/x-www-form-urlencoded; charset=utf-8"0"; 2. Modify the code "$. ajax({...})" can be used.
The operating environment of this tutorial: windows7 system, jquery1.10.0 version. This method is suitable for all brands of computers.
Recommended tutorial: jquery video tutorial
jquery $.post garbled solution:
$.ajaxSetup({ contentType: "application/x-www-form-urlencoded; charset=utf-8" }); $.post("test.php", { name: "i5a6", time: "2pm" }, function(data){ process(data); }, "json");
Or use:
$.ajax({ url:url, type:"POST", data:data, contentType:"application/x-www-form-urlencoded; charset=utf-8", dataType:"json", success: function(){ ... } })
It is recommended to use the first one, but it is also based on your actual situation. Some people recommend using encodeURIComponent for character conversion.
Summarize some experience of garbled data submitted by ajax.
In order to avoid For garbled characters, you can do the following steps
Solution
1. Keep the coding consistent, including file coding, database coding, and web page content-type coding
Check< ;meta http-equiv=”content-type” content=”text/html; charset=UTF-8″ />
It is recommended to use UTF-8 for Chinese, and it may occur when using gbk/gb2312 Garbled code
2, use post to send instead of get
The get method will pass parameters through the link, and will automatically urlEncode (encode), and the encoding method of each browser may not be the same Same. Using post can avoid this situation.
3, by escaping the front-end encoding in js and then sending it, and then decoding it in the background to obtain the data
These can be searched online
4, set the contentType globally and specify the encoding
Because jquery ajax uses utf-8 to encode and send data, IE does not add charset=utf-8 when sending, resulting in garbled characters (IE uses iso-8859-1 encoding by default)
The code is as follows:
$.ajaxSetup({ contentType: "application/x-www-form-urlencoded; charset=utf-8" });
The above is the detailed content of How to solve jquery $.post garbled problem. For more information, please follow other related articles on the PHP Chinese website!