Chinese garbled characters in js and ASP. NET

Source: Internet
Author: User

1. Client-> server issues
1.1. The efficiency of short data submission in get mode is higher than that in post mode.
Reason: my feelings
1.2. When the post method is submitted, if the data contains Chinese characters, the Chinese part of the data obtained by the server will become garbled.
Cause: it may be that XMLHttpRequest automatically encodes non-standard ASCII characters at the time of submission.
It may be a simple conversion, but the specific encoding method is unknown, and it is difficult to restore it on the server.
Solution: (a) manually transcode non-standard ASCII characters in the string using escape () before submission on the client.
This method is suitable for situations where the location of non-standard codes is relatively regular (for example, stored in different variables.
After obtaining the information from the server, you do not need to use unescape () to convert it.
(B) if there are many non-standard codes and it is not convenient to separate escape (), you can use encodeURI () twice (twice, not once ).
The server obtains the original correct content once with decodeURI.
Doubt:
The above two solutions are both correct and feasible after testing.
The question is that when the browser submits data, it looks like a conversion of non-standard codes,
When the server obtains data (such as Request () and getAttribute (), it seems that a reverse conversion is performed.
The two conversions do not seem to follow the same standards, so the default conversion of non-standard codes will lead to incorrect content.
After the client escape (), the server's reverse conversion result is correct. Unfortunately, escape () will convert all
All characters are converted. After the standard ASCII code is converted, it becomes an error (magic...) after being retrieved on the server ....).
Therefore, escape () is only applicable to non-standard code conversion.
The ultimate solution is to encodeURI () twice in a row on the client ().
This rule is obtained by analyzing the transcoded result strings on the server.
For example, the 'medium' character is transcoded to '% E4 % B8 % ad' once after encodeURI (), and manually performed once on the server
DecodeURI () gets garbled characters, wondering if it is an important sign that Request () secretly performs that transcoding and shouldn't be transferred.
'%' Is also removed, so the client performs an encodeURI () operation. In this case, the transcoding result of the 'character in' is
'% 25E4% 25B8% 25AD', which happens to be '%' for 25 h. In this way, the server steals it once and returns '% 25'
'%'. When the string is converted from manual decodeURI (), it has changed to '% E4 % B8 % ad'.
Content.
I didn't seem to make it clear, but I understood it. I hope I can understand it again when I forget it.
2. Server> client Problems
2.1. When turning data containing Chinese characters, the client receives garbled characters
Cause: it must be a page encoding problem, because my premise is not to force the use of unified encoding, so this problem should be solved.
Solution: This is too simple. You only need to set Response. Chartset = "gb2312" anywhere on the server side to write data back to the client,
There is no need for a large-segment transcoding program to be transcoded, as many people have discussed. Of course, if the client uses another encoding method,
Just change it.
2.2. When the client processes the received data in JSON format, the eval () function cannot correctly interpret the received data as a code snippet.
For example, in the form of var obj = eval ("{p1: 1, p2: 2}"), obj cannot be correctly initialized as an object instance, but will
When an error with a semicolon missing is received, an error is generated correctly in the form of eval ("var obj = {p1: 1, p2: 2 }").
Obj.
Actually, if you think about it carefully, it seems that eval () is not directly inserting strings into the entire Code as part of the code, as described in the book.
But return the value of the transferred expression, and the empty function object defined in the form of '{...}', its expression value itself is
Undefined. If there are more than one member, the expression value cannot exist independently as a legal syntax. Therefore, an error is returned;
In the latter form, the essence is actually a value assignment expression. Although the prefix var will cause the entire expression value to be undefined,
However, in this process, the obj object instance is actually generated. References to obj in later contexts are valid.
After the experiment, it seems that the first usage mentioned in some previous articles is actually not working correctly, at least on my machine.
It does fail. Of course, I have to worry about the reason that it may be my browser or even the OS itself.
Solution: no matter how deep the problem is, the problem always needs to be solved. It is also very simple. You only need to put the definition of the receiving variable together in the second form.
To eval (), you can work normally.
In addition, when converting JSON data, we also need to consider the encoding problem of B/S. If the two sides are inconsistent, we can solve the problem by following the method in 2.2.
It is very important that sometimes the debug or trace results, especially the strings, seem correct,
It cannot work normally. At that time, verification should be performed at the encoding level, instead of simply considering the logic of the Code. Because
Some non-print codes are not displayed on the screen during debug and trace operations. "Seeing is not real", this is in any
Always applicable.
Comprehensive experience
As a technology, Ajax itself is not advanced. On the contrary, too much dependence and belief will make it a burden of development.
It is a good thing that the energy is spent in the basic work and the idea is beyond the business logic. It can make your work fail faster.
End.
However, Ajax, as an idea, is worthy of praise. This idea has already been achieved through metersbonwe, who sells children's clothing.
Is not unusual.
Over the past few years,
80% of developers did not expect to find a path beyond the submit. They were happy and on a familiar road.
Another 10% of people are on the iframe road. They are lucky to find a better path.
In addition, 8% of the people found XMLHttpRequest in the grass. They are worthy of respect and they have found a new path for people.
In addition, 2% of people named this new path Ajax. They were great and found the reason for working overtime.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.