The solution to the jquery ajax garbled code is to add the encoding description "header("Content-type:text/html;charset:gbk");" to the header of the file.
Recommendation: "jquery video tutorial"
The operating environment of this tutorial: windows7 system, jquery3.5&&ajax2.0 version, this method is applicable to all brands of computers.
jQuery AJAX Chinese garbled code processing
Recently, jQuery ajax is used to return garbled codes at work. The Notepad editor is used. When the JS part transmits Chinese, another page receives it. If so, garbled characters will appear. I have found many methods on the Internet, but basically there is no good solution.
The page is encoded with GB2312, and the JS part gets Chinese characters: var playerName=$('#playerName').val(); It can be processed like this:
In the ajax code part:
$.ajax({ url: 'getName.php', type: 'GET', cache: false, data:{ 'playerName': encodeURIComponent(playerName)//解决问题的关键点 }, success:function(response){ …… } });
What should be noted here is that jQuery’s ajax handles Chinese in UTF-8 format. It can be processed like this on another page: add
header("Content-type:text/ html;charset:gbk");The gbk here can handle traditional and simplified (need for work^_^), when receiving name:
$playerName=urldecode($_GET['playerName']); $playerName=iconv('utf-8','gbk',$playerName);
Of course, the default encoding of my editor NotePad is ANSI format, this It doesn't have much impact~~.
The week before, I was planning to give up Notepad and switch to Zend for development. After using it for a week, I realized that my Notepad was still better. Why did I feel a little bit like "My first wife is better..." Evil^^.
By the way, the ajax enabler in jquery above has set cache:false. This must be added for IE. Otherwise, if ajax is used, the data will still be the same as before and has not changed. IE caching is too evil. Dear firefox, this situation will not happen^^.
Again, jquery is simply too powerful!
The above is the detailed content of How to solve jquery ajax garbled problem. For more information, please follow other related articles on the PHP Chinese website!