Home Backend Development PHP Tutorial Solution to garbled ajax page and garbled get post_PHP tutorial

Solution to garbled ajax page and garbled get post_PHP tutorial

Jul 13, 2016 am 10:55 AM
a ajax get post and Garbled characters Do of solve page

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...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Five tips to teach you how to solve the problem of Black Shark phone not turning on! Five tips to teach you how to solve the problem of Black Shark phone not turning on! Mar 24, 2024 pm 12:27 PM

As smartphone technology continues to develop, mobile phones play an increasingly important role in our daily lives. As a flagship phone focusing on gaming performance, the Black Shark phone is highly favored by players. However, sometimes we also face the situation that the Black Shark phone cannot be turned on. At this time, we need to take some measures to solve this problem. Next, let us share five tips to teach you how to solve the problem of Black Shark phone not turning on: Step 1: Check the battery power. First, make sure your Black Shark phone has enough power. It may be because the phone battery is exhausted

How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? Where is the automatically saved image when posting? How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? Where is the automatically saved image when posting? Mar 22, 2024 am 08:06 AM

With the continuous development of social media, Xiaohongshu has become a platform for more and more young people to share their lives and discover beautiful things. Many users are troubled by auto-save issues when posting images. So, how to solve this problem? 1. How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? 1. Clear the cache First, we can try to clear the cache data of Xiaohongshu. The steps are as follows: (1) Open Xiaohongshu and click the &quot;My&quot; button in the lower right corner; (2) On the personal center page, find &quot;Settings&quot; and click it; (3) Scroll down and find the &quot;Clear Cache&quot; option. Click OK. After clearing the cache, re-enter Xiaohongshu and try to post pictures to see if the automatic saving problem is solved. 2. Update the Xiaohongshu version to ensure that your Xiaohongshu

The driver cannot be loaded on this device. How to solve it? (Personally tested and valid) The driver cannot be loaded on this device. How to solve it? (Personally tested and valid) Mar 14, 2024 pm 09:00 PM

Everyone knows that if the computer cannot load the driver, the device may not work properly or interact with the computer correctly. So how do we solve the problem when a prompt box pops up on the computer that the driver cannot be loaded on this device? The editor below will teach you two ways to easily solve the problem. Unable to load the driver on this device Solution 1. Search for "Kernel Isolation" in the Start menu. 2. Turn off Memory Integrity, and it will prompt "Memory Integrity has been turned off. Your device may be vulnerable." Click behind to ignore it, and it will not affect the use. 3. The problem can be solved after restarting the machine.

How to solve the problem of garbled characters when importing Chinese data into Oracle? How to solve the problem of garbled characters when importing Chinese data into Oracle? Mar 10, 2024 am 09:54 AM

Title: Methods and code examples to solve the problem of garbled characters when importing Chinese data into Oracle. When importing Chinese data into Oracle database, garbled characters often appear. This may be due to incorrect database character set settings or encoding conversion problems during the import process. . In order to solve this problem, we can take some methods to ensure that the imported Chinese data can be displayed correctly. The following are some solutions and specific code examples: 1. Check the database character set settings In the Oracle database, the character set settings are

How to deal with the problem that Laravel page cannot display CSS correctly How to deal with the problem that Laravel page cannot display CSS correctly Mar 10, 2024 am 11:33 AM

"Methods to handle Laravel pages that cannot display CSS correctly, need specific code examples" When using the Laravel framework to develop web applications, sometimes you will encounter the problem that the page cannot display CSS styles correctly, which may cause the page to render abnormal styles. Affect user experience. This article will introduce some methods to deal with the failure of Laravel pages to display CSS correctly, and provide specific code examples to help developers solve this common problem. 1. Check the file path. First check the path of the CSS file.

How to implement page jump in 3 seconds: PHP Programming Guide How to implement page jump in 3 seconds: PHP Programming Guide Mar 25, 2024 am 10:42 AM

Title: Implementation method of page jump in 3 seconds: PHP Programming Guide In web development, page jump is a common operation. Generally, we use meta tags in HTML or JavaScript methods to jump to pages. However, in some specific cases, we need to perform page jumps on the server side. This article will introduce how to use PHP programming to implement a function that automatically jumps to a specified page within 3 seconds, and will also give specific code examples. The basic principle of page jump using PHP. PHP is a kind of

How to get variables from PHP method using Ajax? How to get variables from PHP method using Ajax? Mar 09, 2024 pm 05:36 PM

Using Ajax to obtain variables from PHP methods is a common scenario in web development. Through Ajax, the page can be dynamically obtained without refreshing the data. In this article, we will introduce how to use Ajax to get variables from PHP methods, and provide specific code examples. First, we need to write a PHP file to handle the Ajax request and return the required variables. Here is sample code for a simple PHP file getData.php:

How to deal with garbled characters in Linux terminal How to deal with garbled characters in Linux terminal Mar 20, 2024 pm 03:12 PM

How to deal with the problem of garbled characters in the Linux terminal. When using the Linux system, sometimes the text displayed in the terminal will be garbled. This brings inconvenience to us when using the terminal and needs to be dealt with in time. This article will introduce how to deal with some common Linux terminal garbled problems, and provide specific code examples. Problem 1: Garbled Chinese characters on the terminal. Garbled Chinese characters on the terminal are usually caused by incorrect character encoding settings on the terminal. We can solve this problem by modifying the terminal's character encoding settings. #View the current terminal

See all articles