Ajax garbled Solution One:
Specify the format for sending data on the server:
In the JSP file:
Response.setcontenttype ("text/text;charset=utf-8")/txt text file returned
Or
Response.setcontenttype ("Text/xml;charset=utf-8")//returned XML file
Php:header ("content-type:text/html;charset=gb2312");
Ajax garbled Solution Two:
PHP send Chinese, Ajax receive
Just add a sentence to the top of PHP:
Header (' content-type:text/html;charset=gb2312 ');
XMLHTTP will correctly parse the Chinese in them.
Ajax send Chinese, PHP receive
This is more complicated:
In Ajax, first use encodeURIComponent to encode the Chinese to be submitted
In PHP:
$GB 2312string=iconv (' UTF-8 ', ' Gb2312//ignore ', $RequestAjaxString);
PHP writes MySQL database
When you set up conn, query the set names "gb2312"
==================================================
Note the red part!!! Otherwise you cannot use!!! Specific reason to guess yourself!
Iconv ("UTF-8", "GBK", $gametypes ["name"]);
Original:
My garbled problem is the AJAX data generated garbled, the original page is the use of GBK code. Later displayed as garbled, I use UE to convert it into UTF-8,
The original elements on the page can be displayed correctly. However, the value of the database is still garbled.
Ob_get_contents ();
Remove all the display data, and then output to the log file, the display is all garbled.
Using the UE to convert the file to GBK and then output, the display is correct.
Use here again: header (' CONTENT-TYPE:TEXT/HTML;CHARSET=GBK ');
The foreground page appears as normal.
After trying this n many methods, many people did not find that the solution is actually very simple, and its answer in our previous processing of JSP garbled. Let's look at the classic Ajax request code first.
Xmlhttp.open ("Post", url, async);
Xmlhttp.setrequestheader ("Content-type", "text/html");
Xmlhttp.send (params);
Through the previous instructions, do not know you now see the clue is not. Do not know is affected by the online tutorials or other aspects of the impact, setRequestHeader and is a constant, and no one wants to change it, and the problem is just out of this place. Recall the encoding setting for the content of a JSP page, which has this section:
Contenttype= "text/html; Charset=utf-8 "
Now that we know the problem, we have to change the second code to read:
Xmlhttp.setrequestheader ("Content-type", "text/html;charset=utf-8");
Finally, don't forget to set it up when you return the data:
Response.setcontenttype ("Text/xml");
Response.setcharacterencoding ("UTF-8");
The several Ajax Chinese garbled solutions mentioned above are all using header (' CONTENT-TYPE:TEXT/HTML;CHARSET=GBK '); This kind of method unifies the page's sending and accepting processing data page code, everybody can now try.