JS and asp.net Chinese garbled problem _ practical skills
Source: Internet
Author: User
1. Client-> service-side problems
1.1. Get way to submit short data efficiency is higher than post mode
Reason: Personal feeling
1.2. Post method submission, if the data contains Chinese, then the data obtained from the service side will become garbled in the Chinese part
Cause: It is possible that XMLHttpRequest ASCII characters were automatically encoded at the time of submission.
May be just a simple escape code conversion, but the specific encoding method is not known, in the service side is difficult to restore.
Workaround: (a) manual transcoding of non-standard ASCII characters in a string before client submission.
This method is more appropriate for the position of the nonstandard code (for example, it is stored in different variables).
It can be processed normally without unescape () conversion after the server is acquired.
(b) The encodeURI () can be used () two times (two times, not once) for multiple and Inconvenient escape ().
The original correct content is obtained once the server is acquired with decodeURI ().
Doubts:
The above two solutions are tested correctly and feasible.
There is a doubt that the browser in the submission of data, it appears to be a non-standard code for a conversion,
When the server is fetched (such as Request (), getattribute (), etc.), it appears that a reverse conversion is done secretly.
These two conversions do not seem to follow the same criteria, so that the default conversion of the nonstandard code will result in the incorrect content being fetched.
After the client escape (), the service-side reversal is correct. Unfortunately escape () will be all convertible in the string
Characters are converted, and standard ASCII conversion, after the service to take out the wrong (magic ...).
So Escape () is only suitable for the transfer of non-standard code.
The ultimate solution is to encodeURI () two consecutive times on the client.
This rule is obtained from the analysis of the result string of the service-side transcoding.
For example, in the word "medium", after the encodeURI () after the transcoding code for '%e4%b8%ad ', and in the service side manually
decodeURI () but got garbled, guess will be Request () secretly carried out that one turn code should not turn the important sign
'% ' also turned off, so in the client to do one more encodeURI (), at this time, the word of the turn-code results became
'%25e4%25b8%25ad ', 25h happens to be '% ', this way, the service side steals a turn, the '%25 ' solution to
'% ', then by manual decodeURI (), the string has become '%e4%b8%ad ', so that the correct
Content.
Seems not to say clearly, but I understand, I hope to forget the time can be read again.
2. Server-side-> client issues
2.1. When rotating the data containing Chinese, the client received a garbled
Reason: It is definitely the problem of page coding, because my premise is not to use a unified code, so this problem to solve.
Solve: Too simple, only need to be in the server to write back to the client anywhere before the Response.chartset = "gb2312" can be set,
Don't need to be like a lot of discussion to transcoding code even someone to write a large section of the transcoding program, of course, the client if it is another way of encoding,
Just change the line.
2.2. When the client processes receiving data in a JSON manner, the eval () function does not correctly interpret the received data as a code fragment
For example, in the form of var obj = eval ("{p1:1, P2:2}"), obj is not properly initialized to an object instance, but is
You receive a missing semicolon error, and in the form of eval ("var obj = {p1:1, p2:2}"), you can correctly generate a
A valid object instance for obj.
In fact, thinking about it, it seems to be right, eval () is not as the book said, directly to the string as part of the code into the entire code
Paragraph, instead of returning the value of the transferred expression, and substituting ' {...} ' is defined as an empty function object whose expression value itself is
Undefined, and if there are more than one member, then this expression value can not be as a legitimate syntax independent existence, so will be an error;
The latter form, in essence, is an assignment expression, although the prefix of Var causes the entire expression value to be undefined,
However, the Obj object instance is actually generated in this process. It is valid to refer to obj in the context that follows.
In the experiment, the first use of the book and some of the predecessors ' articles is actually not working correctly, at least in my machine
, it did fail. Of course, I can't help but think about the reason why my browser, or even the OS itself, is deep.
Solution: No matter how deep, the problem always has to be solved. It is also simple to put the definition of the receiving variable together in the second form
to Eval () to work correctly.
In addition, to rotate the JSON data, we should also consider the B/s coding problem, if not consistent, in 2.2 of the method can be resolved.
It's important to note that sometimes the results of debug or trace, especially strings, look really right, but
is not working properly, it needs to be validated from the coding level, not just the logic of the code itself. Because there are
Some nonprinting encodings that are not displayed back to the screen in Debug and trace. "Seeing is not real", this, in any
Place is always applicable.
Comprehensive feelings
Ajax as a technology, itself is not advanced, on the contrary, too much reliance on and faith will make it a development of the cumbersome, a large number of
The energy spent in the basic work, the idea of drifting away from the business logic, which is a good thing, can make your work faster to fail
Ended.
But, Ajax as a thought, but is worthy of praise, this idea, has long been sold through the children's clothing of the United States to make a penetrating
The generalization--not to go the usual way.
For years, all over the world,
80% of developers did not expect to find their way outside of the submit, they are happy, they walk on a familiar road.
The other 10% were on the way to the IFRAME, and they were fortunate to find a better way to go.
The other 8% found XMLHttpRequest in the bushes, and they were respectable, and they found a new way for people.
Another 2% named the New Road Ajax, they were great, and they found a reason to work overtime to get tired.
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