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.