Solve the problem of garbled gbk files on the JS request server _ javascript skills

Source: Internet
Author: User
This article introduces the garbled problem of the gbk file on the js request server. to solve this problem, you need to set xhr. overrideMimeType (& quot; textcsv; charsetgb2312 & quot, if you need a friend to learn about it, the JS file in gb2312 encoding format is garbled. the ajax network request uses XMLHttpRequest internally. Therefore, you need to set the encoding format before the request, however, xhr is configured. setRequestHeader ("accept", "text/csv; charset = gb2312, */*"); no effect. only xhr is set. overrideMimeType ("text/csv; charset = gb2312"); is correct, the code is as follows:

$. 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 );}}});

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.

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.