Solution to garbled ajax page and garbled get post_PHP tutorial

WBOY
Release: 2016-07-13 10:55:01
Original
877 people have browsed it

Solution to garbled ajax page and get post garbled code
In the past, ASP pages had various garbled characters. When the page was refreshed, the codes were garbled or the links were garbled. Last night, I asked my mother-in-law, and she came up with a solution. Add <% @ CODEPAGE = "65001" LANGUAGE = before all ASP pages. "VBSCRIPT" %> <% Response.CODEPAGE = 65001%> , 65001 refers to the UTF-8 encoding format GB2312 is 936, the reason is that other programs did not declare Response.CodePage when you entered the UTF-8 page. Session.CodePage is assigned a value immediately (65001 or 936 is assigned a different value depending on the version), and then when entering another page, the Response.CodePage of the other page is immediately assigned a value by Session.CodePage. So if that page gets @ CODEPAGE = 936 If the Response.CodePage of your page is assigned a value of 65001, it will definitely be garbled, so write the code uniformly at the beginning of the page.

Then last night, I encountered another problem. When AJAX returned to Chinese, it displayed garbled characters. The encoding of the two pages was the same UTF-8. Why did garbled characters appear? I checked Du Niang and said that in the AJAX processing page, add <% Response.Charset = "UTF-8"%> The result page does not display the content. I have studied it and it is probably a data encoding problem in the database tutorial, because the ACCESS data encoding is based on the encoding when you write the data. I wrote it in. When it was GB2312, now using UTF-8 format encoding and decoding will definitely cause garbled characters. What should I do if I set <% Response.Charset = "GB2312" %> and then AJAX Chinese will return to normal. Here is the explanation of the Charset attribute in W3C It appends the character set name to the content-type header of the page's Response object.

Add <% Response.Charset = "GB2312"%>

Page output display

 <%content-type:text/html; charset=GB2312%>

I guess Charset changed the character set to GB2312 so it can accept GB2312 encoded data so it won’t be garbled


Take a look at the ajax solution to get and post garbled characters

There are two types of garbled data in terms of program execution. One is that the Chinese text sent to the background program itself is garbled, because xmlHTTP follows the word processing mechanism of web page special effects and uses UTF-8 encoding. But if the background page uses GB2312 or other types of encoding, the received data will naturally be garbled. Another problem is that when data is received and returned, characters are garbled. This is also caused by the difference between the encoding used by the background page and the Javascript encoding. The characters returned by the server script will use the server encoding such as GB2312 by default. The problem of garbled data sent back by the server is easy to solve. We only need to add a file header that defines the encoding on the page where the data is sent back by the server.
When defining file header information, depending on the script, you can use the following methods:
PHP:header("Content-Type:text/html;charset=GB2312");
ASP:Response.Charset("GB2312")
JSP:response.setHeader("Charset","GB2312");
(Other scripts can search for their related class libraries, and usually have functions or methods for setting header information.)
After talking about sending back messages, let’s talk about the problem of sending garbled messages. In fact, regardless of the encoding, the Chinese characters we input will be sent to the server correctly in UTF-8 format. However, when the server receives it, it is not decoded in the way we expected, but uses the server's default characters. Encoding method, usually GB2312 to decode information. Then the characters we see are naturally wrong.
We all know that XMLHTTP has two ways of sending data, one is GET and the other is POST. The garbled code of GET is relatively simple to solve. Just add a header information that defines the encoding: setrequestheader("Content-Type", "text/html; encoding=gb2312"). In this way, the data sent by GET method will be correctly understood by the server script as GB2312 method and decoded.
The more difficult part is when using the POST method to send data, the above method is invalid because the Content-type used by the POST data is: xmlObj.setrequestheader("Content-Type","application/x-www-form -urlencoded"); No character encoding is defined. I have encountered great difficulties in solving this problem, but I have found two better solutions so far. The first is to URL-encode the Chinese characters before sending them to the server. That is, use: encodeURI().
But it should be noted that this method needs to be used twice. The first time is to process the characters into URL encoding. The second time is to encode the encoded data again. This is done because if the encoding is sent for the first time, the server will automatically decode it. If so, we have no way to control the decoding method we want. So encodeURL needs to be performed twice. The purpose of this is that the data we obtain on the server side is a string after encodedURL. Then we use the corresponding decoding function of the server-side script to restore the string. In this way, we get the correct string we want. The disadvantage of this is that we have to exponentially increase the amount of data sent. You know, as the amount of data increases, the chance of errors increases.
Another way is to use cookies to dump data, which does not increase the data but requires certain browser permissions. Although reading and writing cookies is very easy. But I think this operation is not ideal. I can't say much without testing it. And I think there should be a third way to solve the problem of garbled characters.

Solution to AJAX POST garbled characters in PHP!
Although it is not advanced or handsome, it finally solved the problem. The solution to the problem is officially due to PHP's complete function library! Just one iconv() function solves the problem of garbled characters!
On the client side, no settings are required. Just get the normal value and send it to the server side using xmlHTTP and then accept the value normally on the server side. Just use the iconv() function to re-encode the string after receiving the value!
$R_Guest = strval(iconv("UTF-8","GB2312",$_POST["R_Guest"]));
$R_Content = strval(iconv("UTF-8","GB2312",$_POST["R_Content"]));

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632279.htmlTechArticleAjax page garbled code and get post garbled solution. Before doing ASP page, various garbled codes were created. When the page was refreshed, the page was garbled or the link was garbled. Garbled code. I asked Du Niang last night and came up with a solution. On all ASP pages...
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