SoFunction
Updated on 2025-03-01

Solve the problem of garbled gbk file requesting JS server

When JS obtains a file with a 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 setting ("accept", "text/csv;charset=gb2312,*/*"); has no effect. Only setting ("text/csv;charset=gb2312"); is correct, and the code is as follows:

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

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

ps: js Chinese display garbled code or garbled code on page solution

①.js file displays garbled code in Chinese

There is a coding scheme (such as GBK) when editing and saving Javascript files. When the encoding scheme used when opening a file (such as UTF-8) is inconsistent with the encoding scheme during storage, garbled Chinese will appear.

Solution:

(1) When opening browsing with the editor, select the same encoding method as the original file encoding (such as GBK) to view it, and there will be no garbled code;

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

②The Chinese fields in JavaScript in the website page are garbled

Chinese in Chinese is displayed normally in eclipse or editor. Call it on the page. After execution, the Chinese fields in the page result are displayed in garbled code.

Solution:

"Show" the file encoding method on the referenced page to keep it consistent with the file itself (such as GBK).

<script type="text/javascript" language="JavaScript" src="/js/" charset=“GBK”></script>

suggestion:

(1) When creating and editing with Eclipse or Myeclipse, it is recommended to change the Text file encoding to UTF-8 first, and then edit and save.
(2) Edit and save with text documents or other editors. It is recommended to save in UTF-8 encoding.
(3) "Show" on the referenced page to add the encoding method of the file UTF-8.

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