Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the problem of Chinese garbled code when introducing external js files in asp.net

Shulou Source: shulou.com Published: 2022-06-02 12:22:52 10月04日 Update

This article is about how to solve the Chinese garbled problem of introducing external js files in asp.net. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

In asp.net, you often need to call js code, if js is written in asp.net, there is generally no garbled problem. Because they have the same code. There is a risk of garbled code if you need to reference external javascript code. In general, the page codes of Asp.net and Javascript are either GB2312 or Utf-8, and garbled will occur if the two codes are inconsistent. The solution is to unify the coding of the two. In general, the encoding of Asp.net to accept and send requests is Utf-8, and the javascript encoding is Gb2312 by default, so it is prone to problems.

You can set the accept send code in two places in Asp.net

Add to 1.Web.config

Add to the 2.Aspx page file

3. Add the following sentence to the page load event:

Response.ContentEncoding = System.Text.Encoding.GetEncoding ("GB2312")

Today, I am writing AJAX code, but there is a very strange problem, that is, all Chinese has become garbled. It has been studied for a long time and checked on the Internet. Basically, the default coding for AJAX asynchronous transmission is UTF-8, while the default coding for asp.net is not this, which is why there is such a garbled problem.

So using. Net Encoding to do a lot of coding conversion experiments, the result is still garbled.

Finally, he was discouraged and asked Louis to take a look. Louis did a very simple test and inserted a sentence into my js code: alert ("Chinese")

It should be able to display in this way, because it has not been processed by asp.net. But guess what? Unexpectedly, it is also garbled. Depressed. Louis said they had encountered this problem before and finally checked out that it was a problem with the encoding of js files. And then I stopped studying when I was getting off work.

When I got home, I carefully did an experiment and copied the contents of the .js file directly into the script tag in the .aspx file, so that it could be displayed normally. Haha. It is said on the Internet that javascript is encoded by utf-8, so I saved the code of the .js file as utf-8, and it was a success. The specific approach is to open a js file in vs2003, save it as, do not change the file name when saving, keep the original, to ensure that the original file is overwritten, and choose the encoding method for the signed UTF-8.

Thank you for reading! This is the end of this article on "how to solve the problem of Chinese garbled code in external js files in asp.net". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Tags: Coding file garbled problem Chinese code that is content result page Chinese plus situation time more article UTF-8 Utf-8 utf-8 research experiment Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Redmi Docker NVidia OPPO Reno