Home > Web Front-end > JS Tutorial > body text

How to solve jquery ajax garbled problem

藏色散人
Release: 2020-11-30 10:59:52
Original
2230 people have browsed it

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.

How to solve jquery ajax garbled problem

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){
     ……
   }
});
Copy after login

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);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template