Home > Web Front-end > JS Tutorial > Solve the problem of JS requesting server gbk file garbled_javascript skills

Solve the problem of JS requesting server gbk file garbled_javascript skills

WBOY
Release: 2016-05-16 15:36:23
Original
1482 people have browsed it

When JS obtains a file with the server encoding format gb2312, the content is garbled. The ajax network request uses XMLHttpRequest internally, so you need to set the encoding format before requesting, but set xhr.setRequestHeader("accept", "text/csv ;charset=gb2312,*/*"); has no effect. Only setting xhr.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定义全局变量 
     //  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> 
Copy after login

The overrideMimeType function will override the header sent to the server, forcing text/csv;charset=gb2312 as the mime-type.

ps: Solution to js displaying garbled characters in Chinese or displaying garbled characters on the page

①.js file displays garbled characters in Chinese

The Javascript file XX.js has an encoding scheme (such as GBK) when editing and saving. When the encoding used when opening the file (such as UTF-8) is inconsistent with the encoding scheme when saving, garbled Chinese characters will appear.

Solution:

(1) When opening and browsing with an editor, select the encoding method that is consistent with the original file encoding (such as GBK) to view, then there will be no garbled characters;

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

②Garbled Chinese fields in JavaScript in the website page

Chinese characters in XX.js are displayed normally in eclipse or the editor. When calling XX.js on the page, after execution, the Chinese field in the page results displays garbled characters.

Solution:

"Display" the encoding method of the XX.js file on the page that references XX.js, so that it is consistent with the encoding method of the XX.js file itself (such as GBK).

Recommendation:

(1) When using Eclipse or Myeclipse to create XX.js and edit it, it is recommended to change the Text file encoding to UTF-8 first, and then edit and save it.
(2) Use a text document or other editor to edit and save XX.js. It is recommended to save it in UTF-8 encoding.
(3) "Display" and add the encoding method UTF-8 of the XX.js file on the page that references XX.js.

<script type="text/javascript" language="JavaScript" src="/js/XX.js" charset=“utf-8”></script>
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template