<legend id='I6QFW'><style id='I6QFW'><dir id='I6QFW'><q id='I6QFW'></q></dir></style></legend>
<tfoot id='I6QFW'></tfoot>
  • <small id='I6QFW'></small><noframes id='I6QFW'>

      <i id='I6QFW'><tr id='I6QFW'><dt id='I6QFW'><q id='I6QFW'><span id='I6QFW'><b id='I6QFW'><form id='I6QFW'><ins id='I6QFW'></ins><ul id='I6QFW'></ul><sub id='I6QFW'></sub></form><legend id='I6QFW'></legend><bdo id='I6QFW'><pre id='I6QFW'><center id='I6QFW'></center></pre></bdo></b><th id='I6QFW'></th></span></q></dt></tr></i><div id='I6QFW'><tfoot id='I6QFW'></tfoot><dl id='I6QFW'><fieldset id='I6QFW'></fieldset></dl></div>
        <bdo id='I6QFW'></bdo><ul id='I6QFW'></ul>

        JavaScript Fetch:有编码问题的字符

        时间:2023-10-01
          <tbody id='HRhQe'></tbody>

        <i id='HRhQe'><tr id='HRhQe'><dt id='HRhQe'><q id='HRhQe'><span id='HRhQe'><b id='HRhQe'><form id='HRhQe'><ins id='HRhQe'></ins><ul id='HRhQe'></ul><sub id='HRhQe'></sub></form><legend id='HRhQe'></legend><bdo id='HRhQe'><pre id='HRhQe'><center id='HRhQe'></center></pre></bdo></b><th id='HRhQe'></th></span></q></dt></tr></i><div id='HRhQe'><tfoot id='HRhQe'></tfoot><dl id='HRhQe'><fieldset id='HRhQe'></fieldset></dl></div>

              <tfoot id='HRhQe'></tfoot>
            1. <small id='HRhQe'></small><noframes id='HRhQe'>

                <legend id='HRhQe'><style id='HRhQe'><dir id='HRhQe'><q id='HRhQe'></q></dir></style></legend>
                  <bdo id='HRhQe'></bdo><ul id='HRhQe'></ul>

                  本文介绍了JavaScript Fetch:有编码问题的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用 Fetch 将一些数据带入屏幕,但是一些字符显示出奇怪的符号,我认为这与转换特殊字符有关.

                  I'm trying to use Fetch to bring some data into the screen, however some of the characters ares showing a weird sign which I believe has something to do with converting special chars.

                  在服务器端调试或在浏览器上调用 servlet 时,问题不会发生,因此我认为问题出在我的 JavaScript 上.请看下面的代码:

                  When debugging on the server side or if I call the servlet on my browser, the problem doesn't happen, so I believe the issue is with my JavaScript. See the code below:

                  var myHeaders = new Headers();
                  myHeaders.append('Content-Type','text/plain; charset=UTF-8');
                  
                  fetch('getrastreiojadlog?cod=10082551688295', myHeaders)
                          .then(function (response) {
                              return response.text();
                          })
                          .then(function (resp) {
                              console.log(resp);
                          });

                  我认为这可能是一些细节,但我还没有设法找出发生了什么.所以欢迎任何提示谢谢

                  I think it is probably some detail, but I haven't managed to find out what is happening. So any tips are welcome Thx

                  推荐答案

                  response 的 text() 函数总是将 payload 解码为 utf-8.

                  The response's text() function always decodes the payload as utf-8.

                  如果您想要其他字符集中的文本,您可以使用 TextDecoder 转换 响应缓冲区 (NOT文本)转换为具有所选字符集的解码文本.

                  If you want the text in other charset you may use TextDecoder to convert the response buffer (NOT the text) into a decoded text with chosen charset.

                  使用您的示例应该是:

                  var myHeaders = new Headers();
                  myHeaders.append('Content-Type','text/plain; charset=UTF-8');
                  
                  fetch('getrastreiojadlog?cod=10082551688295', myHeaders)
                          .then(function (response) {
                              return response.arrayBuffer();
                          })
                          .then(function (buffer) {
                              const decoder = new TextDecoder('iso-8859-1');
                              const text = decoder.decode(buffer);
                              console.log(text);
                          });
                  

                  请注意,我使用 iso-8859-1 作为解码器.

                  Notice that I'm using iso-8859-1 as decoder.

                  致谢:施耐德博客

                  这篇关于JavaScript Fetch:有编码问题的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:Fetch API 如何获取响应体? 下一篇:如何使用 then() 将 Fetch 响应的 JSON 主体传递给 Throw Error()?

                  相关文章

                  1. <tfoot id='75Jel'></tfoot>

                    <small id='75Jel'></small><noframes id='75Jel'>

                      <i id='75Jel'><tr id='75Jel'><dt id='75Jel'><q id='75Jel'><span id='75Jel'><b id='75Jel'><form id='75Jel'><ins id='75Jel'></ins><ul id='75Jel'></ul><sub id='75Jel'></sub></form><legend id='75Jel'></legend><bdo id='75Jel'><pre id='75Jel'><center id='75Jel'></center></pre></bdo></b><th id='75Jel'></th></span></q></dt></tr></i><div id='75Jel'><tfoot id='75Jel'></tfoot><dl id='75Jel'><fieldset id='75Jel'></fieldset></dl></div>
                        <bdo id='75Jel'></bdo><ul id='75Jel'></ul>
                      <legend id='75Jel'><style id='75Jel'><dir id='75Jel'><q id='75Jel'></q></dir></style></legend>