Solve the Problem of garbled gbk file on the JS request server, jsgbk

Source: Internet
Author: User

Solve the Problem of garbled gbk file on the JS request server, jsgbk

When JS obtains a file whose server encoding format is gb2312, the content is garbled. The ajax network request uses XMLHttpRequest internally. Therefore, you need to set the encoding format before the request, but set xhr. setRequestHeader ("accept", "text/csv; charset = gb2312, */*"); no effect. Only xhr is set. overrideMimeType ("text/csv; charset = gb2312"); is correct, the Code is as follows:

<Span style = "font-size: 18px;"> $. ajax ({type: "get", url: "http://7xnhdv.com1.z0.glb.clouddn.com/test1.csv", beforeSend: function (xhr) {// beforeSend defines the global variable // xhr. setRequestHeader ("accept", "text/csv; charset = gb2312, */*"); xhr. overrideMimeType ("text/csv; charset = gb2312") ;}, success: function (xmlDoc, textStatus, xhr) {if (xhr. status = 200) {$ ('# view0 '). text (xmlDoc) ;}}); </span>

OverrideMimeType overwrites the header sent to the server and forces text/csv; charset = gb2312 as the mime-type.

Ps: js Chinese display Garbled text or display Garbled text on the page Solution

① Garbled Chinese characters in the. js File

Javascript file XX. there is a encoding scheme (such as GBK) for js editing and saving. When the encoding (such as UTF-8) used when opening the file is inconsistent with the encoding scheme when saving, chinese characters are displayed with garbled characters.

Solution:

(1) When you open the browser in the editor, select the encoding method consistent with the original file encoding (such as GBK), and no garbled characters will appear;

(2) Select the XX garbled characters in the package explorer of eclipse. js, right-click the menu, select properties, and select XX in Text file encoding. the encoding (such as GBK) during js editing and saving can be displayed normally;

② Chinese fields in JavaScript on the website page are garbled

In eclipse or editor XX. js, Chinese characters are displayed normally, and XX. js is called on the page. After execution, Chinese fields in the results of the page are garbled.

Solution:

Add the encoding method of the XX. js file on the page that references XX. js to make it consistent with the encoding method (such as GBK) of the XX. js file.

<Script type = "text/javascript" language = "JavaScript" src = "/js/XX. js" charset = "GBK"> </script>

Suggestion:

(1) In Eclipse or Myeclipse new XX. js, for editing, it is recommended to change Text file encoding to UTF-8, and then edit save.
(2) Save XX. js with text document or other editor editing, it is recommended to save in UTF-8 encoding.
(3) On the page that references XX. js "show" plus the encoding method UTF-8 of XX. js file.

<script type="text/javascript" language="JavaScript" src="/js/XX.js" charset=“utf-8”></script>

Related Article

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.