Chinese garbled characters of external JS files introduced in Asp.net

Source: Internet
Author: User
The introduction of external JS files in asp.netasp.net is prone to Chinese garbled characters and solutions are discussed.

JS is often called in Asp.net.CodeIf JS is written in Asp.net, it will not be garbled. Because they have the same encoding. If you need to reference external JavaScript code, there is a risk of garbled code. Under normal circumstances Asp.net and JavaScript page encoding is not gb2312 is the Utf-8, if the two encoding inconsistency will produce garbled. The solution is to unify the encoding of the two. Since Asp.net generally accepts the encoding for sending requests is Utf-8, the default JavaScript encoding is gb2312, so it is prone to problems.

In Asp.net, you can set sending encoding in two places.

1. Add in Web. config

2. Add the following content to the ASPX page file:

3. Add the following sentence to the page loading event:
Response. contentencoding = system. Text. encoding. getencoding ("gb2312 ");

I am writing Ajax code today, but it is strange that all Chinese characters are garbled. After studying for a long time, also checked on the internet, basically is Ajax asynchronous transmission when the default encoding is UTF-8, and Asp.net default encoding is not this, this is why such a garbled problem occurs.

So I used encoding in. Net to perform a lot of encoding conversion experiments. The results are still garbled.

Finally, I was disheartened and asked Louis for help. Louis made a simple test and inserted a sentence in my JS Code: Alert ("Chinese ");

This should be visible because it has not been processed by Asp.net. But what do you guess? It's also garbled... depressing. Louis said they had encountered this problem before and finally checked out that it was a JS file encoding problem. I will not study it any more soon.

Back home, I did a careful test and copied the content in the. js file directly to the script tag in the. aspx file. Then, it can be displayed normally after running. Haha. I said on the Internet that JavaScript is UTF-8 encoded, so I saved the. js file encoding as UTF-8, and the result was successful. The specific method is to open a JS file in vs2003 and save it as. Do not change the file name when saving the file. Keep the original file to overwrite the original file, and select the encoding method for the signed UTF-8.

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.